[记]linux Qt的手动构建
例1:简单的计算器
1.工程目录Calculator下的文件包含
CalculatorDialog.h、CalculatorDialog.cpp、main.cpp
2.各文件内容
CalculatorDialog.h

#ifndef __CALCULLATORDIALOG_H #define __CALCULLATORDIALOG_H #include <QDialog> #include <QLabel> #include <QPushButton> #include <QLineEdit>//行编辑控件 #include <QHBoxLayout>//水平布局器 #include <QDoubleValidator>//验证器 class CalculatorDialog:public QDialog { Q_OBJECT //moc private: /* data */ QLineEdit* m_editX;//左操作数 QLineEdit* m_editY;//右操作数 QLineEdit* m_editZ;//显示结果 QLabel* m_label;//"+" QPushButton* m_button;//"=" public: CalculatorDialog(void); ~CalculatorDialog(); public slots: //使能等号按钮的槽操作数 void enableButton(void); //计算结果和显示的槽函数 void calcClicked(void); }; CalculatorDialog::CalculatorDialog(/* args */) { } CalculatorDialog::~CalculatorDialog() { } #endif //__CALCULLATORDIALOG_H
CalculatorDialog.cpp

#include "CalculatorDialog.h" //构造函数 CalculatorDialog::CalculatorDialog(void) { //界面初始化 setWindowTitle("计算器");//设置窗口标题 //左操作数,this即为当前父窗口指针 m_editX = new QLineEdit(this); //设置文本对齐:右对齐 m_editX->setAlignment(Qt::AlignRight); //设置数字验证器,只能输入数字形式内容 m_editX->setValidator(new QDoubleValidator(this)); //右操作数,this即为当前父窗口指针 m_editY = new QLineEdit(this); //设置文本对齐:右对齐 m_editY->setAlignment(Qt::AlignRight); //设置数字验证器,只能输入数字形式内容 m_editY->setValidator(new QDoubleValidator(this)); //显示结果 m_editZ = new QLineEdt(this); m_editZ->setAlignment(Qt::AlignRight); m_editZ->setReadOnly(true);//设置只读 //"+" m_label = new QLabel("+",this); //"=" m_button = new QPushButton("=",this); m_button->setEnabled(false); //创建布局器:自动调用每个控件的大小和位置 QHBoxLayout* layout = new QHBoxLayout(this); //按水平方向,依次将控件添加到布局器中 layout->addWidget(m_editX); layout->addWidget(m_label); layout->addWidget(m_editY); layout->addWidget(m_button); layout->addWidget(m_editZ); //设置布局器 setLayout(layout); //信号和槽函数连接 //左右操作数文本改变时,发送信号textChanged connect(m_editX,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void))); connect(m_editY,SIGNAL(textChanged(QString)),this,SLOT(enableButton(void))); //点击按钮,发送信号clicked connect(m_button,SIGNAL(clicked(void))),this,SLOT(calcClicked(void)); } //使能等号按钮的槽操作数 void CalculatorDialog::enableButton(void) { bool bXOk,bYOk; //text():获取输入文本(QString) //toDouble():QString转换为double,参数保存转换是否成功结果 m_editX->text().toDouble(&bXOk); m_editY->text().toDouble(&bYOk); //当左右操作数都输入了有效数据,则使能等号按钮,否则设置禁用 m_button->setEnabled(bXOk&&bYOk); } //计算结果和显示的槽函数 void CalculatorDialog::calcClicked(void) { double res = m_editX->text().toDouble()+m_editY->text().toDouble(); //number():将double转换为QString QString str = QString::number(res); //显示字符串形式结果 m_editZ->setText(str); }
main.cpp

#include <QApplication> #include "CalculatorDialog.h" int main(int argc,char** argv) { QApplication app(argc,argv); CalculatorDialog calc; calc.show(); return app.exec(); }
3.手动构建过程
在当前路径下
qmake -project
生成Calculator.pro文件

# #自动生成,by qmake # TEMPLATE = app TARGET = Calculator INCLUDEPATH += . # Input HEADERS += CalculatorDialog.h SOURCES += CalculatorDialog.cpp main.cpp
打开Calculator.pro文件,并加入包含的部件

# #自动生成,by qmake # QT += widgets TEMPLATE = app TARGET = Calculator INCLUDEPATH += . # Input HEADERS += CalculatorDialog.h SOURCES += CalculatorDialog.cpp main.cpp .pro
然后执行qmake生成c++源码、makefile文件
执行make进行编译
执行生成的二进制文件 。/Calculator
例2:获取系统时间
1.工程目录Time下的文件包含
TimeDialog.h、TimeDialog.cpp、main.cpp
2.各文件内容
TimeDialog.h

#ifndef __TIMEDIALOG_H #define __TIMEDIALOG_H #include <QDialog> #include <QLabel> #include <QPushButton> #include <QVBoxLayout>//垂直布局器 #include <QTime>//时间 #include <QDebug>//打印调试 class TimeDialog:public QDialog { Q_OBJECT //moc private: /* data */ QLabel* m_label;//显示时间label QPushButton* m_button;//获取时间button public slots: //获取系统时间的槽函数 void getTime(void); public: //构造函数 TimeDialog(void); ~TimeDialog(); }; TimeDialog::TimeDialog(/* args */) { } TimeDialog::~TimeDialog() { } #endif//__TIMEDIALOG_H
TimeDialog.cpp

#include "TimeDialog.h" #include <QFont> TimeDialog::TimeDialog(void) { //初始化界面 //显示时间 m_label = new QLabel(this); //设置label边框消息:凹陷面板 m_label->setFrameStyle(QFrame::Pannel|QFrame::Sunken); //设置label文本对齐方式:水平/垂直居中 m_label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter); //设置label的字体大小 QFont font; font.setPointSize(20); m_label->setFont(font); //获取系统时间的按钮 m_button = new QPushButton("获取当前时间",this); m_button->setFont(font); //创建垂直布局器 QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(m_label); layout->addWidget(m_button); //设置布局器 setLayout(layout); //信号和槽函数连接 connect(m_button,SIGNAL(clicked()),this,SLOT(getTime(void))); } //获取系统时间的槽函数 void TimeDialog::getTime(void) { qDebug("getTime"); qDebug() << "getTime"; //获取当前系统时间 QTime time = QTime::currentTime(); //将时间对象转换为字符串 QString str=time.toString("hh:mm:ss"); //显示时间 m_label->setText(str); }
main.cpp

#include <QApplication> #include "TimeDialog.h" int main(int argc,char** argv) { QApplication app(argc,argv); TimeDialog time; time.show(); return app.exec(); }
3.手动构建过程
当前目录执行 qmake -project -->Time.pro
qmake -->Makefile
编辑Time.pro --> Qt+=Qwigets
执行make 完成编译和链接
3.单文件项目构建
1.工程目录main下的文件包含
main.cpp
2.文件内容
main.cpp

#include <QApplication> #include <QLabel> int main(int argc,char** argv) { //创建Qt应用对象 QApplication app(argc,argv); //创建标签控件 QLabel label("hello QT"); //显示标签控件 label.show(); //进入应用循环 return app.exec(); }
3.手动构建过程
执行qmke -project -->在当前目录下生成工程文件 main.pro (包含头文件 源文件 库的位置、添加的部件)
|QT += widgets (添加的模块【QLabel】)
| TEMPLATE = app (模板名 app)
| TARGET = main (目标程序名,和项目名一致)
| INCLUDEPATH += . (包含当前目录下的头文件)
| SOURCES += main.cpp (添加的源文件)
执行qmake--> 根据.pro文件生成Makefile文件
执行make -->完成编译和链接(若使用多文件面对对象,其间会生成moc_xxx.h,moc_xxx.cpp文件)
-------------====================分割线====================-------------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!