Und hier ist der Code
main.cpp
Code:
#include <QApplication>
#include <QPushButton>
#include <QWidget>
#include <QFileDialog>
#include <iostream>
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
private slots:
void slotButtonClicked();
private:
QPushButton *m_button;
};
Window::Window(QWidget *parent)
: QWidget(parent)
{
// Set size of the window
setFixedSize(100, 50);
// Create and position the button
m_button = new QPushButton("Hello World", this);
m_button->setGeometry(10, 10, 80, 30);
connect(m_button, SIGNAL (clicked()), this, SLOT (slotButtonClicked()));
}
void Window::slotButtonClicked()
{
QString fname = QFileDialog::getOpenFileName(
this,
tr("Open File"),
"/home", "");
std::cout << fname.toUtf8().constData() << std::endl;
}
#include "main.moc"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
Window wnd;
wnd.show();
return app.exec();
}
CMakeLists.txt
Code:
cmake_minimum_required(VERSION 2.8.11)
PROJECT(test C CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets REQUIRED)
include_directories(${Qt5Widgets_INCLUDE_DIRS})
SET(SRCS
main.cpp
)
SET(LIBS
${Qt5Widgets_LIBRARIES}
)
# Executable
ADD_EXECUTABLE(qt-test ${SRCS} )
# Linking
TARGET_LINK_LIBRARIES(qt-test ${LIBS})
Dateisystem schaut so aus:
Code:
georg@xps:/tmp$ tree qttest/
qttest/
├── CMakeLists.txt
└── main.cpp
0 directories, 2 files
Bauen
Code:
georg@xps:/tmp$ cd qttest
georg@xps:/tmp/qttest$ mkdir build
georg@xps:/tmp/qttest$ cd build/
georg@xps:/tmp/qttest/build$ cmake ..
-- The C compiler identification is GNU 5.3.1
-- The CXX compiler identification is GNU 5.3.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/qttest/build
georg@xps:/tmp/qttest/build$ make
Scanning dependencies of target qt-test_automoc
[ 25%] Automatic moc for target qt-test
Generating main.moc
[ 25%] Built target qt-test_automoc
Scanning dependencies of target qt-test
[ 50%] Building CXX object CMakeFiles/qt-test.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/qt-test.dir/qt-test_automoc.cpp.o
[100%] Linking CXX executable qt-test
[100%] Built target qt-test
georg@xps:/tmp/qttest/build$
AUsführen:
Code:
georg@xps:/tmp/qttest/build$ ./qt-test
- - - Aktualisiert - - -
Du must auch nicht die IDE nutzen. Ich nutze Emacs und cmake. cmake weil diese makefiles leicht zu schreiben sind und die Makros die Libs und Includes leicht finden.
Lesezeichen