七度黑光--当感到一切都毫无头绪的时候,最好静下心来从基础开始.

qt学习最简单的例子

2008-04-08 13:14:24 / 个人分类:linux学习笔记

/*
hello.cpp
ubuntu 7.10
*/

#include <QApplication>
#include <QPushButton>
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>

int main(int argc, char *argv[])

{
        QApplication app(argc, argv);
        QPushButton* hello=new QPushButton(QObject::tr("hello world!"),0);

        QWidget* pWidget = new QWidget;
        QLabel label(pWidget);
        QPushButton* btn = new QPushButton(QObject::tr("close"), pWidget);
        QVBoxLayout* layout = new QVBoxLayout;
        layout->addWidget(&label);
        layout->addWidget(btn);
        layout->addWidget(hello);
        pWidget->setLayout(layout);
        QObject::connect(btn, SIGNAL(clicked()), pWidget, SLOT(close()));
        pWidget->show();
        return app.exec();
}

编译步骤:
helight@helight-desktop:~/linux/qt-test/hello$ qmake -project
helight@helight-desktop:~/linux/qt-test/hello$ qmake
helight@helight-desktop:~/linux/qt-test/hello$ make
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_SHARED -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o hello.o hello.cpp
g++  -o hello hello.o    -L/usr/lib -lQtGui -lQtCore -lpthread
helight@helight-desktop:~/linux/qt-test/hello$ ./hello





TAG: linux学习笔记

ranfow ranfow 发布于2008-04-09 23:54:21
我的是qt-3.3.8b-2.fc8
我查过了
QApplication =>qapplication
QPushButton => qpushbutton
QWidget         => qwidget
QLabel            =>
没有QVBoxLayout
七度黑光 helight 发布于2008-04-09 12:54:27
你的qt没有安装么?忘了说了,这是qt4.3.2的.
ranfow ranfow 发布于2008-04-09 12:45:44
[ranfow@localhost qt]$ make
g++ -c -pipe -Wall -W -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -DQT_NO_DEBUG  -I/usr/lib/qt-3.3/mkspecs/default -I. -I. -I/usr/lib/qt-3.3/include -o hello.o hello.cpp
hello.cpp:1:24: error: QApplication: No such file or directory
hello.cpp:2:23: error: QPushButton: No such file or directory
hello.cpp:3:19: error: QWidget: No such file or directory
hello.cpp:4:23: error: QVBoxLayout: No such file or directory
hello.cpp:5:18: error: QLabel: No such file or directory
hello.cpp: In function ‘int main(int, char**)’:
hello.cpp:9: error: ‘QApplication’ was not declared in this scope
hello.cpp:9: error: expected `;' before ‘app’
hello.cpp:10: error: ‘QPushButton’ was not declared in this scope
hello.cpp:10: error: ‘hello’ was not declared in this scope
hello.cpp:10: error: expected type-specifier before ‘QPushButton’
hello.cpp:10: error: expected `;' before ‘QPushButton’
hello.cpp:12: error: ‘QWidget’ was not declared in this scope
hello.cpp:12: error: ‘pWidget’ was not declared in this scope
hello.cpp:12: error: expected type-specifier before ‘QWidget’
hello.cpp:12: error: expected `;' before ‘QWidget’
hello.cpp:14: error: ‘QLabel’ was not declared in this scope
hello.cpp:14: error: expected `;' before ‘label’
hello.cpp:15: error: ‘btn’ was not declared in this scope
hello.cpp:15: error: expected type-specifier before ‘QPushButton’
hello.cpp:15: error: expected `;' before ‘QPushButton’
hello.cpp:16: error: ‘QVBoxLayout’ was not declared in this scope
hello.cpp:16: error: ‘layout’ was not declared in this scope
hello.cpp:16: error: expected type-specifier before ‘QVBoxLayout’
hello.cpp:16: error: expected `;' before ‘QVBoxLayout’
hello.cpp:18: error: ‘label’ was not declared in this scope
hello.cpp:22: error: expected primary-expression before ‘>’ token
hello.cpp:22: error: ‘setLayout’ was not declared in this scope
hello.cpp:24: error: ‘QObject’ has not been declared
hello.cpp:24: error: ‘clicked’ was not declared in this scope
hello.cpp:24: error: ‘SIGNAL’ was not declared in this scope
hello.cpp:24: error: ‘close’ was not declared in this scope
hello.cpp:24: error: ‘SLOT’ was not declared in this scope
hello.cpp:27: error: ‘app’ was not declared in this scope
hello.cpp: At global scope:
hello.cpp:7: warning: unused parameter ‘argc’
hello.cpp:7: warning: unused parameter ‘argv’
make: *** [hello.o] Error 1
someone someone 发布于2008-04-08 19:26:57
这不简单啊,隐含了好多头文件,lib库,linux下编程实际上很大一个弊病,相对于windows来说
就是缺乏SDK,虽然有qtdesigner ,不过编译环境还是要自己设

这里如果是新手,谁会自己去搭建
qt的编译环境?
那可需要很长时间啊
我来说两句

(可选)

Open Toolbar