当前位置: 代码迷 >> QT开发 >> 新手C++ GUI Qt4 编程 第二章的一个例子
  详细解决方案

新手C++ GUI Qt4 编程 第二章的一个例子

热度:503   发布时间:2016-04-25 03:08:06.0
新手求教:C++ GUI Qt4 编程 第二章的一个例子
我电脑是ubuntukylin14.04
g++4.8.2
Qt 5.3

书上第二章的例子,我按书上的例子写了,然后编译各种报错,有网友说是头文件包含QT4和5不一样的问题(反正我没搞明白。。。)

然后我想先弄个简单的,就先创建这样一个界面出来,等成功了再添加型号和槽等,然后应为头文件包含的问题,我把所有代码写在一个文件里了
#include<QApplication>
#include<QPushButton>
#include<QLabel>
#include<QLineEdit>
#include<QCheckBox>
#include<QDialog>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QWidget>
class FindDialog:public QDialog
{
 Q_OBJECT
 private:
  QPushButton* find_button;
  QPushButton* close_button;
  QLabel* label;
  QLineEdit* line_edit;
  QCheckBox* case_box;
  QCheckBox* search_box;  //设置个窗体部件 
 public:
  FindDialog(QWidget* parent = 0); //构造函数 
};
FindDialog::FindDialog(QWidget* parent):QDialog(parent)
{
 //创建部件 
  find_button = new QPushButton("Find");
  close_button = new QPushButton("Close");
  find_button->setDefault(true);
  find_button->setEnabled(false);

  label = new QLabel("Find &what");
  
  line_edit = new QLineEdit;
  
  case_box = new QCheckBox("Match &case");
  search_box = new QCheckBox("search *backword");
//设置个部件布局布局 
  QHBoxLayout* left_layout = new QHBoxLayout;
  left_layout->addWidget(label);
  left_layout->addWidget(line_edit);

  QVBoxLayout* top_layout = new QVBoxLayout;
  top_layout->addLayout(left_layout);
  top_layout->addWidget(case_box);
  top_layout->addWidget(search_box);

  QVBoxLayout* r_layout = new QVBoxLayout;
  r_layout->addWidget(find_button);
  r_layout->addWidget(close_button);

  QHBoxLayout* main_layout = new QHBoxLayout;
  main_layout->addLayout(top_layout);
  main_layout->addLayout(r_layout);

  setLayout(main_layout);
//设置标题 
  setWindowTitle("Find");
}
int main(int argc , char* argv[])
{
  QApplication app(argc,argv);
  FindDialog* dialog = new FindDialog;
  dialog->show();
  return app.exec();
}


代码这样,然后文件位于/home/../QT_train/2-1/find.cxx
然后命令qmake -project
得到一个文件2-1.pro,我用vim打开这个文件在里面添加QT += widgets
保存后退出
然后命令 qmake
得到一个Makefile文件,到这里一切正常

然后命令make

系统提示
find.o:在函数‘FindDialog::FindDialog(QWidget*)’中:
find.cxx:(.text+0x17):对‘vtable for FindDialog’未定义的引用
collect2: error: ld returned 1 exit status
make: *** [2-2-1] 错误 1
------解决思路----------------------
把Q_OBJECT那一行注释掉。
  相关解决方案