Qt/Boost

From ProgrammingExamples
< Qt
Revision as of 12:02, 15 February 2011 by Daviddoria (Talk | contribs)

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

main.cpp

#include <QApplication>
#include <iostream>
 
#include "form.h"
 
#include <boost/signals2/signal.hpp>
 
void func()
{
  std::cout << "Hello, world!" << std::endl;
}
 
int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  MyForm form;
 
  form.show();
 
  boost::signals2::signal<void ()> s;
  s.connect(func);
 
  return app.exec();
}


form.h

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

form.cpp

#include "form.h"
 
MyForm::MyForm(QWidget *parent)
    : QWidget(parent)
{
  setupUi(this);
  connect( this->pushButton, SIGNAL( clicked() ), this, SLOT(pushButton_SetLabelText()) );
}
 
void MyForm::pushButton_SetLabelText()
{
  this->label->setText("hello");
}

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>My Program</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>130</x>
     <y>100</y>
     <width>91</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>Click me</string>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>150</x>
     <y>150</y>
     <width>61</width>
     <height>18</height>
    </rect>
   </property>
   <property name="text">
    <string>Before click</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>


CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(Boost)
 
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE(${QT_USE_FILE})
 
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DQT_NO_KEYWORDS")
 
QT4_WRAP_UI(UISrcs form.ui)
QT4_WRAP_CPP(MOCSrcs form.h)
 
include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
 
ADD_EXECUTABLE(Boost main.cpp form.cpp ${MOCSrcs} ${UISrcs})
TARGET_LINK_LIBRARIES(Boost ${QT_LIBRARIES})