1.使用场景
在Qt页面开发中,使用uiDesigner布局页面。
使用ui文件时,一般在setupUi(QWidget )函数中,就是我们在designer拖拽的界面的自动布局,狭义上讲,
使用ui文件,一定程度上让代码的逻辑部分与ui部分隔离,新建一个ui designer Form,放置一个QPushButton对象,
然后在ui designer中右键转到槽。其中setupUi(QWidget)中的代码如下
1.1 ui设计器生成代码预览
在类的构造函数中,转定义,setupUi(QWidget*)函数源码如下
/********************************************************************************
** Form generated from reading UI file 'widget.ui'
**
** Created by: Qt User Interface Compiler version 5.14.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QPushButton *pushButton;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(690, 336);
pushButton = new QPushButton(Widget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(110, 90, 491, 171));
retranslateUi(Widget);
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
pushButton->setText(QCoreApplication::translate("Widget", "buttonForTest", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
2.代码分析
需要注意的部分大致分为三部分,都是设计器自动生成的代码。
2.1 namespace定义的必要性
namespace Ui{class Widget: public Ui_Widget {};}代码声明了一个Ui的域,域中声明一个Widget类型的类继承至Ui_Widget类,假设,此时创建的Qt设计师界面类名字也叫Widget,这两个Widget是两个东西,使用域区分,一个是Ui::Widget,一个是Widget类,假设不这样设计,直接在Widget类中内嵌一个Ui_Widget类的对象
,那么当ui designer文件被修改时,ui_Widget也被修改,再次编译会引起Widget也被重新编译,Ui::Widget类是Qt做好的,用户可以不用关心
2.2 为什么使用retranslateUi(QWidget *widget)
函数内大量使用QCoreApplication::translate(),这其实是为Qt多语言设计提供了便利性,当页面需要支持多语言时,会需要动态切换语言,当加载完翻译文件时,需要手动更新页面组件的文案,这时候对于在designer设计器中控件,只需要手动调用ui->retranslateUi(this)即可完成页面翻译,在designer之外的控件,就需要一个个手动调用 widget->setText(tr("UI测试"));
2.3 QMetaObject::connectSlotsByName(Widget);函数的作用
在上述ui_widget.h中,没有显示地调用connect函数去连接ui中的pushButton,函数作用是根据信号的发送者,和信号名自动链接,源代码如下
void QMetaObject::connectSlotsByName(QObject *o)
{
if (!o)
return;
const QMetaObject *mo = o->metaObject();
Q_ASSERT(mo);
const QObjectList list = // list of all objects to look for matching signals including...
o->findChildren<QObject *>(QString()) // all children of 'o'...
<< o; // and the object 'o' itself
// for each method/slot of o ...
for (int i = 0; i < mo->methodCount(); ++i) {
const QByteArray slotSignature = mo->method(i).methodSignature();
const char *slot = slotSignature.constData();
Q_ASSERT(slot);
// ...that starts with "on_", ...
if (slot[0] != 'o' || slot[1] != 'n' || slot[2] != '_')
continue;
// ...we check each object in our list, ...
bool foundIt = false;
for(int j = 0; j < list.count(); ++j) {
const QObject *co = list.at(j);
const QByteArray coName = co->objectName().toLatin1();
// ...discarding those whose objectName is not fitting the pattern "on_<objectName>_...", ...
if (coName.isEmpty() || qstrncmp(slot + 3, coName.constData(), coName.size()) || slot[coName.size()+3] != '_')
continue;
const char *signal = slot + coName.size() + 4; // the 'signal' part of the slot name
// ...for the presence of a matching signal "on_<objectName>_<signal>".
const QMetaObject *smeta;
int sigIndex = co->d_func()->signalIndex(signal, &smeta);
if (sigIndex < 0) {
// if no exactly fitting signal (name + complete parameter type list) could be found
// look for just any signal with the correct name and at least the slot's parameter list.
// Note: if more than one of thoses signals exist, the one that gets connected is
// chosen 'at random' (order of declaration in source file)
QList<QByteArray> compatibleSignals;
const QMetaObject *smo = co->metaObject();
int sigLen = qstrlen(signal) - 1; // ignore the trailing ')'
for (int k = QMetaObjectPrivate::absoluteSignalCount(smo)-1; k >= 0; --k) {
const QMetaMethod method = QMetaObjectPrivate::signal(smo, k);
if (!qstrncmp(method.methodSignature().constData(), signal, sigLen)) {
smeta = method.enclosingMetaObject();
sigIndex = k;
compatibleSignals.prepend(method.methodSignature());
}
}
if (compatibleSignals.size() > 1)
qWarning() << "QMetaObject::connectSlotsByName: Connecting slot" << slot
<< "with the first of the following compatible signals:" << compatibleSignals;
}
if (sigIndex < 0)
continue;
// we connect it...
if (Connection(QMetaObjectPrivate::connect(co, sigIndex, smeta, o, i))) {
foundIt = true;
// ...and stop looking for further objects with the same name.
// Note: the Designer will make sure each object name is unique in the above
// 'list' but other code may create two child objects with the same name. In
// this case one is chosen 'at random'.
break;
}
}
if (foundIt) {
// we found our slot, now skip all overloads
while (mo->method(i + 1).attributes() & QMetaMethod::Cloned)
++i;
} else if (!(mo->method(i).attributes() & QMetaMethod::Cloned)) {
// check if the slot has the following signature: "on_..._...(..."
int iParen = slotSignature.indexOf('(');
int iLastUnderscore = slotSignature.lastIndexOf('_', iParen-1);
if (iLastUnderscore > 3)
qWarning("QMetaObject::connectSlotsByName: No matching signal for %s", slot);
}
}
}
从上述代码中,不难看出,自动链接信号槽的规则是on_<objectName>_<signal>,因此,只需要在widget.h中声明一个槽函数
void on_pushButton_clicked();
就可以完成连接,在visual studio 开发中,无一键右键转到槽函数,有时,当使用uidesigner时,恰好将槽函数声明为on_<objectName>_<signal>的形式,但又手动使用connect()连接一次,这会导致该按钮的信号槽被注册两次,当点击时,槽函数也会被调用两次,因此在vs开发中,如果槽函数使用了该命名规则,就无需再手动使用connect连接。
3.小知识分享
1.信号槽的默认连接方式是自动链接,自动连接的含义是:如果槽函数所在的线程和信号发出的线程在同一线程,即使用直接链接,如果信号槽在不同的线程中,则使用队列链接。
2.直接链接的含义是,当信号发出时,槽函数立即被执行,且在信号发出的线程中执行
3.队列链接的含义是,当信号发出时,只有槽函数所在的线程获得所有权时才会执行槽函数
4.组赛连接只能用在信号和槽不在同一线程的情况,若在同线程,会造成死锁