Size: 774
Comment:
|
← Revision 4 as of 2014-01-29 18:23:55 ⇥
Size: 4271
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 35: | Line 35: |
== Sample CMake + Qt == Adapted from http://cor-net.org/wp-content/uploads/2009/10/HelloWorldQt.tar Structure: * CMakeLists.txt * src\HelloWidget.cpp * src\HelloWidget.h * src\MainWindow.cpp * src\MainWindow.h * src\main.cpp File CMakeLists.txt {{{#!highlight cmake CMAKE_MINIMUM_REQUIRED(VERSION 2.6) PROJECT(HelloWorldQt) SET(CMAKE_BUILD_TYPE Release) SET(CMAKE_CXX_FLAGS "-Wall") # QT4 Handling FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) SET( HWQ_Qt4_SRC src/MainWindow.h src/HelloWidget.h ) SET( HWQ_Qt4_UI ) SET( HWQ_Qt4_RES ) QT4_WRAP_CPP(HWQ_MOC_CPP ${HWQ_Qt4_SRC}) QT4_WRAP_UI(HWQ_UI_CPP ${HWQ_Qt4_UI}) QT4_ADD_RESOURCES(HWQ_RES_H ${HWQ_Qt4_RES}) INCLUDE_DIRECTORIES( . ) # General SET( HWQ_SRC src/main.cpp src/MainWindow.cpp src/HelloWidget.cpp ${HWQ_MOC_CPP} ${HWQ_UI_CPP} ${HWQ_RES_H} ) SET( HWQ_LIB ${QT_LIBRARIES} ) ADD_EXECUTABLE(HelloWorldQt ${HWQ_SRC} ) TARGET_LINK_LIBRARIES(HelloWorldQt ${HWQ_LIB} ) INSTALL_TARGETS( /bin HelloWorldQt) }}} File HelloWidget.cpp {{{#!highlight cpp #include "HelloWidget.h" HelloWidget::HelloWidget(QWidget * parent) : QLabel(parent), _msg("Hello World !"), _pos(0) { } HelloWidget::~HelloWidget() { } void HelloWidget::showNewLetter() { _pos++; setText(_msg.left(_pos)); } }}} File HelloWidget.h {{{#!highlight cpp #include <QLabel> #include <QString> #ifndef HELLOWIDGET_H_ #define HELLOWIDGET_H_ class HelloWidget : public QLabel { Q_OBJECT public: HelloWidget(QWidget * parent = 0); ~HelloWidget(); public slots: void showNewLetter(); protected: QString _msg; unsigned short _pos; }; #endif /* HELLOWIDGET_H_ */ }}} File MainWindow.cpp {{{#!highlight cpp #include "MainWindow.h" #include <QVBoxLayout> MainWindow::MainWindow(QWidget *parent) { // Making a central Widget for our Window QWidget * centralWidget = new QWidget(this); setCentralWidget(centralWidget); // Building our widgets _helloWidget = new HelloWidget(this); _button = new QPushButton(this); _button->setText("++"); connect(_button, SIGNAL(clicked()), _helloWidget, SLOT(showNewLetter())); // Setting the content and disposition of our central Widget QVBoxLayout * layout = new QVBoxLayout(); layout->addWidget(_helloWidget); layout->addWidget(_button); centralWidget->setLayout(layout); } MainWindow::~MainWindow() { } }}} File MainWindow.h {{{#!highlight cpp #include <QtGui/QMainWindow> #include <QPushButton> #include "HelloWidget.h" #ifndef MAINWINDOW_H_ #define MAINWINDOW_H_ class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); protected: HelloWidget * _helloWidget; QPushButton * _button; }; #endif /* MAINWINDOW_H_ */ }}} File main.cpp {{{#!highlight cpp #include <QtGui/QApplication> #include "MainWindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } }}} === Build === * find . -name "*.cmake" | xargs -i rm {} * cmake . * make clean * make * make install |
qt
Qt is a cross-platform application and UI framework for developers
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:
- CMakeLists.txt
src\HelloWidget.cpp
src\HelloWidget.h
src\MainWindow.cpp
src\MainWindow.h
- src\main.cpp
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
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
Build
- find . -name "*.cmake" | xargs -i rm {}
- cmake .
- make clean
- make
- make install