Qt/Dialog

From ProgrammingExamples
< Qt
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.h

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

form.cpp

#include <QtGui>
#include <iostream>
 
#include "form.h"
#include "dialog.h"
 
Form::Form(QWidget *parent)
    : QWidget(parent)
{
  setupUi(this);
}
 
void Form::on_pushButton_clicked()
{
  this->label->setText(QString::number(this->horizontalSlider->value()));
  std::cout << this->label->text().toDouble() << std::endl;
 
  // Using this, the user can continue interacting with both windows
  Dialog* myDialog(new Dialog);
  myDialog->exec();
}

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="QSlider" name="horizontalSlider">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>70</y>
     <width>160</width>
     <height>22</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>


dialog.h

#ifndef DIALOG_H
#define DIALOG_H
 
#include "ui_dialog.h"
 
class Dialog : public QDialog, private Ui::Dialog
{
  Q_OBJECT
public:
  Dialog();
 
};
 
#endif

dialog.cpp

#include "dialog.h"
 
Dialog::Dialog()
{
  setupUi(this);
}

dialog.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QDialogButtonBox" name="buttonBox">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>240</y>
     <width>341</width>
     <height>32</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>140</x>
     <y>100</y>
     <width>67</width>
     <height>17</height>
    </rect>
   </property>
   <property name="text">
    <string>TextLabel</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>buttonBox</sender>
   <signal>accepted()</signal>
   <receiver>Dialog</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>248</x>
     <y>254</y>
    </hint>
    <hint type="destinationlabel">
     <x>157</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>buttonBox</sender>
   <signal>rejected()</signal>
   <receiver>Dialog</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>316</x>
     <y>260</y>
    </hint>
    <hint type="destinationlabel">
     <x>286</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

CMakeLists.txt

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