现在最新版的QT都会自配有qtcreater这个IDE辅助开发人员进行UI设计和程序编写,但有的系统版本(如ARM、aarch64)并无qtcreater,所以想要使用linux下QT的qmake编译代码,如果没有可以在官网上面安装一个或者自己编译一个(怎么编译可以参考以前写过的文章:【QT】aarch64(ARM)系统下编译aarch64(ARM)版本的QT),博主的qmake就存放在以下目录中:
博主准备了四个源文件,分别如下:
其中mainwindow.ui 是QT的UI界面文件,博主只是在里面加了两个按钮,代码如下:
-
<?xml version="2.0" encoding="UTF-8"?>
-
<ui version="4.0">
-
<class>MainWindow
</class>
-
<widget class="QMainWindow" name="MainWindow">
-
<property name="geometry">
-
<rect>
-
<x>0
</x>
-
<y>0
</y>
-
<width>362
</width>
-
<height>130
</height>
-
</rect>
-
</property>
-
<property name="windowTitle">
-
<string>MainWindow
</string>
-
</property>
-
<widget class="QWidget" name="centralwidget">
-
<widget class="QPushButton" name="pushButton">
-
<property name="geometry">
-
<rect>
-
<x>20
</x>
-
<y>20
</y>
-
<width>141
</width>
-
<height>31
</height>
-
</rect>
-
</property>
-
<property name="text">
-
<string>千万不要点这个按钮
</string>
-
</property>
-
</widget>
-
<widget class="QPushButton" name="pushButton_2">
-
<property name="geometry">
-
<rect>
-
<x>180
</x>
-
<y>20
</y>
-
<width>161
</width>
-
<height>31
</height>
-
</rect>
-
</property>
-
<property name="text">
-
<string>旁边这个也不能按呀喂!
</string>
-
</property>
-
</widget>
-
</widget>
-
<widget class="QMenuBar" name="menubar">
-
<property name="geometry">
-
<rect>
-
<x>0
</x>
-
<y>0
</y>
-
<width>362
</width>
-
<height>23
</height>
-
</rect>
-
</property>
-
</widget>
-
<widget class="QStatusBar" name="statusbar"/>
-
</widget>
-
<resources/>
-
<connections/>
-
</ui>
然后就是main.cpp ,主要负责构造一个UI类启动界面等一些初始化工作:
-
#include "mainwindow.h"
-
-
#include <QApplication>
-
-
int main(int argc, char *argv[])
-
{
-
QApplication a(argc, argv);
-
MainWindow w;
-
w.show();
-
return a.exec();
-
}
紧接着就是mainwindow.cpp ,按钮触发事件实现代码以及一些析构构造实现函数:
-
#include "mainwindow.h"
-
#include "ui_mainwindow.h"
-
-
MainWindow::MainWindow(QWidget *parent)
-
: QMainWindow(parent)
-
, ui(
new Ui::MainWindow)
-
{
-
ui->setupUi(
this);
-
}
-
-
MainWindow::~MainWindow()
-
{
-
delete ui;
-
}
-
-
-
void MainWindow::on_pushButton_clicked()
-
{
-
QMessageBox::information(
NULL,
"点了呀",
"点了其实也没啥~");
-
}
-
-
void MainWindow::on_pushButton_2_clicked()
-
{
-
QMessageBox::warning(
NULL,
"点了呀",
"点就点了");
最后就是它的头文件mainwindow.h :
-
#ifndef MAINWINDOW_H
-
#define MAINWINDOW_H
-
-
#include <QMainWindow>
-
#include <QMessageBox>
-
-
QT_BEGIN_NAMESPACE
-
namespace Ui {
class MainWindow; }
-
QT_END_NAMESPACE
-
-
class MainWindow :
public QMainWindow
-
{
-
Q_OBJECT
-
-
public:
-
MainWindow(QWidget *parent =
nullptr);
-
~MainWindow();
-
-
private slots:
-
void on_pushButton_clicked();
-
-
void on_pushButton_2_clicked();
-
-
private:
-
Ui::MainWindow *ui;
-
};
-
#endif // MAINWINDOW_H
准备好以上文件之后我们就执行以下指令:
-
/opt/Qt5
.12
.10/
5.12
.10/gcc_64/bin/qmake -project
"QT+=widgets"
-
//博主的qmake路径是这个,每个人的qmake路径不同,需要按照实际情况来
至于这些参数是什么意思,可以参考QT官方给出的解释,下面给出部分解释:
-
QMake has two modes, one mode for generating project files based on
-
some heuristics, and the other for generating makefiles. Normally you
-
shouldn't need to specify a mode, as makefile generation is the default
-
mode for qmake, but you may
use this
to
test qmake
on an existing
project
-
-
Mode:
-
-
project Put qmake
into
project
file generation
mode
-
In this
mode qmake interprets [files]
as files
to
-
be added
to the .pro file.
By
default,
all files
with
-
known
source extensions
are added.
-
Note: The created .pro
file probably will
-
need
to be edited.
For example
add the QT
variable
to
-
specify what modules
are required.
-
-makefile Put qmake
into makefile generation
mode (
default)
-
In this
mode qmake interprets files
as
project files
to
-
be processed,
if skipped qmake will try
to find a
project
-
file
in your
current working
directory
指令执行结束后会多出来一个.pro为后缀的工程文件:
如果说你的程序里面还依赖了其他的一些什么库,可以在工程文件里面把连接也加上来,比如博主需要用到同样路径下的libtest.so这个动态库则:
而后在执行一次qmake指令:
/opt/Qt5.12.10/5.12.10/gcc_64/bin/qmake
然后就会多出来一个Makefile:
最后make一下就可以生成一个可执行文件:
执行下程序就发现是可以运行的:
注:当你出来各种各样的错误时,有没有想过是权限问题或者是路径问题?
转载:https://blog.csdn.net/qq_41884002/article/details/117112563