1 QTDesigner贼简单,俗称有手就行
这是qtdesigner,我们先堆一个界面出来
然后我们回到这个xml去观测一下发生了哪些变化:
<?xml version="1.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>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>521</width>
<height>41</height>
</rect>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>实验一</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>90</x>
<y>10</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>实验二</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>170</x>
<y>10</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>实验三</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_4">
<property name="geometry">
<rect>
<x>250</x>
<y>10</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>实验四</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_5">
<property name="geometry">
<rect>
<x>330</x>
<y>10</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>实验五</string>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menuNX">
<property name="title">
<string>NX</string>
</property>
</widget>
<addaction name="menuNX"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
如果你写过java的Android,你就十分熟悉了,这就是它那一套,简直一模一样,只是这个QT垃圾编译器,体验贼差
我们来看一下,一个窗口怎么去加载这个xml,其实加载xml的过程还是通过new出栈对象去去堆界面,只是,它是通过反射的方法,如果你感兴趣可以去阅读我的c#和java编程技巧里面的反射这几个章节,没啥东西,老技术了
看一下,它这种写法很奇葩,我理解是自动创建了一个ui,所以我改写
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->ui = new Ui::MainWindow();
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
这样看起来就不是很奇葩,我们可以理解为创建了一个一个来自于命名空间Ui的MainWindow()的堆对象,析构的时候记得删除它,否则内存泄露。然后,我们看到在Ui命名空间下的MainWindow()对象拥有setupUi(QMainWindow *parent)这样一个接口,就是安装的意思,所以,如果我们要在Sx这个Widget里面使用刚刚创建的那个.ui文件,我们就得去new一个Ui命名空间下的MainWindow()对象去安装这xml
怎么做呢?
a.sx.h引入Ui的命名空间,同时,因为setupUi的参数是QMainWindow,所以不能继承自QWidget,得继承自QMainWindow
#ifndef SX_H
#define SX_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
#include<QWidget>
class Sx : public QMainWindow
{
Q_OBJECT
public:
Sx();
void mousePressEvent(QMouseEvent *event);
void debug(const char* value);
Ui::MainWindow *ui;
signals:
void clickEvent_001();
public slots:
void clickEvent_001Process();
};
#endif // SX_H
b.定义一个Ui命名空间下的MainWindow()指针 *ui;
#include "sx.h"
#include<QMouseEvent>
#include<QMessageBox>
#include<QObject>
#include "ui_mainwindow.h"
#include "mainwindow.h"
Sx::Sx()
{
this->ui = new Ui::MainWindow();
ui->setupUi(this);
}
void Sx::mousePressEvent(QMouseEvent *event){
emit this->clickEvent_001();
}
void Sx::clickEvent_001Process(){
}
void Sx::debug(const char *value){
QMessageBox::information(this,"Info",value,QMessageBox::Ok);
}
c.显示窗口:
#include "mainwindow.h"
#include <QApplication>
#include"sx.h"
#include<QHBoxLayout>
#include<QPushButton>
#include<QRect>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Sx w;
w.show();
return a.exec();
}
这样就搞定了
下一章我们讲,怎么去获取按钮并绑定它的事件