qt

Qt is a cross-platform application and UI framework for developers

http://qt-project.org/

Sample C++ Qt program in Slackware Linux

   1 /* 
   2 Include path in Slackware 14:
   3 /usr/lib/qt/include/Qt
   4 
   5 Compile on Slackware 14: 
   6 g++ -lQtCore -lQtGui -I/usr/lib/qt/include/Qt helloWorldQt.cpp -o helloWorldQt
   7 
   8 Run with ./helloWorldQt
   9 */
  10 
  11 #include <qapplication.h>
  12 #include <qmainwindow.h>
  13 #include <qpushbutton.h>
  14 
  15 int main(int argc,char** argv){
  16     QApplication app(argc,argv);
  17     QMainWindow mw;
  18     mw.setMinimumSize(200,100);
  19     mw.setMaximumSize(200,100);
  20 
  21     QPushButton pb("Push it, Hello world",&mw);
  22     pb.setGeometry(20,20,160,60);
  23     mw.setCentralWidget(&pb);
  24     mw.show();
  25     return app.exec();
  26 }

Sample CMake + Qt

Adapted from http://cor-net.org/wp-content/uploads/2009/10/HelloWorldQt.tar

Structure:

File CMakeLists.txt

   1 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
   2 PROJECT(HelloWorldQt)
   3 
   4 SET(CMAKE_BUILD_TYPE Release)
   5 SET(CMAKE_CXX_FLAGS "-Wall")
   6 
   7 # QT4 Handling
   8 FIND_PACKAGE(Qt4 REQUIRED)
   9 
  10 INCLUDE(${QT_USE_FILE})
  11 
  12 SET( HWQ_Qt4_SRC
  13         src/MainWindow.h
  14         src/HelloWidget.h
  15 )
  16 SET( HWQ_Qt4_UI
  17 )
  18 SET( HWQ_Qt4_RES
  19 )
  20 
  21 QT4_WRAP_CPP(HWQ_MOC_CPP ${HWQ_Qt4_SRC})
  22 QT4_WRAP_UI(HWQ_UI_CPP ${HWQ_Qt4_UI})
  23 QT4_ADD_RESOURCES(HWQ_RES_H ${HWQ_Qt4_RES})
  24 
  25 INCLUDE_DIRECTORIES( . )
  26 
  27 # General
  28 SET( HWQ_SRC
  29         src/main.cpp
  30         src/MainWindow.cpp
  31         src/HelloWidget.cpp
  32         ${HWQ_MOC_CPP}
  33         ${HWQ_UI_CPP}
  34         ${HWQ_RES_H}
  35 )
  36 
  37 SET( HWQ_LIB
  38         ${QT_LIBRARIES}
  39 )
  40 
  41 ADD_EXECUTABLE(HelloWorldQt ${HWQ_SRC} )
  42 TARGET_LINK_LIBRARIES(HelloWorldQt ${HWQ_LIB} )
  43 
  44 INSTALL_TARGETS( /bin HelloWorldQt)

File HelloWidget.cpp

   1 #include "HelloWidget.h"
   2 
   3 HelloWidget::HelloWidget(QWidget * parent) :
   4         QLabel(parent),
   5         _msg("Hello World !"),
   6         _pos(0) {
   7 }
   8 
   9 HelloWidget::~HelloWidget() {
  10 }
  11 
  12 void HelloWidget::showNewLetter() {
  13         _pos++;
  14         setText(_msg.left(_pos));
  15 }

File HelloWidget.h

   1 #include <QLabel>
   2 #include <QString>
   3 
   4 #ifndef HELLOWIDGET_H_
   5 #define HELLOWIDGET_H_
   6 
   7 class HelloWidget : public QLabel {
   8         Q_OBJECT
   9 
  10 public:
  11         HelloWidget(QWidget * parent = 0);
  12         ~HelloWidget();
  13 
  14 public slots:
  15         void showNewLetter();
  16 
  17 protected:
  18         QString _msg;
  19         unsigned short _pos;
  20 };
  21 
  22 #endif /* HELLOWIDGET_H_ */
  23 

File MainWindow.cpp

   1 #include "MainWindow.h"
   2 #include <QVBoxLayout>
   3 
   4 MainWindow::MainWindow(QWidget *parent) {
   5         // Making a central Widget for our Window
   6         QWidget * centralWidget = new QWidget(this);
   7         setCentralWidget(centralWidget);
   8 
   9         // Building our widgets
  10         _helloWidget = new HelloWidget(this);
  11         _button = new QPushButton(this);
  12         _button->setText("++");
  13 
  14         connect(_button, SIGNAL(clicked()), _helloWidget, SLOT(showNewLetter()));
  15 
  16         // Setting the content and disposition of our central Widget
  17         QVBoxLayout * layout = new QVBoxLayout();
  18         layout->addWidget(_helloWidget);
  19         layout->addWidget(_button);
  20         centralWidget->setLayout(layout);
  21 }
  22 
  23 MainWindow::~MainWindow() {
  24 }

File MainWindow.h

   1 #include <QtGui/QMainWindow>
   2 #include <QPushButton>
   3 #include "HelloWidget.h"
   4 
   5 #ifndef MAINWINDOW_H_
   6 #define MAINWINDOW_H_
   7 
   8 class MainWindow : public QMainWindow {
   9         Q_OBJECT
  10 
  11 public:
  12         MainWindow(QWidget *parent = 0);
  13         ~MainWindow();
  14 
  15 protected:
  16         HelloWidget * _helloWidget;
  17         QPushButton * _button;
  18 };
  19 
  20 #endif /* MAINWINDOW_H_ */
  21 

File main.cpp

   1 #include <QtGui/QApplication>
   2 #include "MainWindow.h"
   3 
   4 int main(int argc, char *argv[])
   5 {
   6         QApplication a(argc, argv);
   7         MainWindow w;
   8         w.show();
   9         return a.exec();
  10 }

Build

qt (last edited 2014-01-29 18:23:55 by bl13-7-37)