Qt/Widgets/RadioButton

From ProgrammingExamples
< Qt
Revision as of 08:57, 15 January 2013 by Daviddoria (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

main.cpp

#include <QApplication>
 
#include "form.h"
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Form form;
 
    form.show();
    return app.exec();
}

form.cpp

#include <QtGui>
 
#include "form.h"
 
Form::Form(QWidget *parent)
    : QWidget(parent)
{
  ui.setupUi(this);
  connect( this->ui.pushButton, SIGNAL( clicked() ), this, SLOT(pushButton_SetLabelText()) );
  connect( this->ui.radioButton, SIGNAL( clicked() ), this, SLOT(radioButton_SetLabelText()) );
  connect( this->ui.radioButton_2, SIGNAL( clicked() ), this, SLOT(radioButton_2_SetLabelText()) );
}
 
void Form::pushButton_SetLabelText()
{
 
  if(this->ui.radioButton->isChecked())
  {
    this->ui.label->setText("1 selected");
  }
 
  if(this->ui.radioButton_2->isChecked())
  {
      this->ui.label->setText("2 selected");
  }
 
}
 
void Form::radioButton_SetLabelText()
{
 
  if(this->ui.radioButton->isChecked())
  {
    this->ui.label->setText("1 clicked");
  }
 
}
 
void Form::radioButton_2_SetLabelText()
{
 
  if(this->ui.radioButton_2->isChecked())
  {
    this->ui.label->setText("2 clicked");
  }
 
}

form.h

#ifndef FORM_H
#define FORM_H
 
#include "ui_form.h"
 
class Form : public QWidget
{
    Q_OBJECT
 
public:
    Form(QWidget *parent = 0);
 
  public slots:
    void pushButton_SetLabelText();
    void radioButton_SetLabelText();
    void radioButton_2_SetLabelText();
 
private:
    Ui::Form ui;
};
 
#endif

form.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>120</y>
     <width>91</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>PushButton</string>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>170</x>
     <y>220</y>
     <width>61</width>
     <height>18</height>
    </rect>
   </property>
   <property name="text">
    <string>TextLabel</string>
   </property>
  </widget>
  <widget class="QRadioButton" name="radioButton">
   <property name="geometry">
    <rect>
     <x>250</x>
     <y>90</y>
     <width>105</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>RadioButton</string>
   </property>
  </widget>
  <widget class="QRadioButton" name="radioButton_2">
   <property name="geometry">
    <rect>
     <x>250</x>
     <y>130</y>
     <width>105</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>RadioButton</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(RadioButton)
 
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
 
QT4_WRAP_UI(UISrcs form.ui)
QT4_WRAP_CPP(MOCSrcs form.h)
 
include_directories(${CMAKE_CURRENT_BINARY_DIR})
 
ADD_EXECUTABLE(RadioButton main.cpp form.cpp ${MOCSrcs} ${UISrcs})
TARGET_LINK_LIBRARIES(RadioButton ${QT_LIBRARIES})