一、designer
Ubuntu命令行输入
designer
若无法运行,则需重新链接,方法如下:
参考这个方法进行修复
二、界面
打开后界面如下:三、Qt designer中无法输入中文的解决办法
(1)方法一:别的地方写好,复制粘贴过来
四、添加了环境变量
注意:book为用户名,需替换为自己的用户名
vi /home/book/.bashrc
添加:
export QTDIR=/home/book/Qt/5.15.2/gcc_64
export PATH=$QTDIR/bin:$PATH
修改后,执行:
注意:book为用户名,需替换为自己的用户名
source /home/book/.bashrc
完成。
之后便可以使用一些命令,比如:
uic Calcultor2.ui -o ui_Calculator2.h
五、UI使用方式
目录结构:
Calcultor2.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CalculatorDialog</class>
<widget class="QDialog" name="CalculatorDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>724</width>
<height>318</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
</font>
</property>
<property name="windowTitle">
<string>计算器</string>
</property>
<widget class="QPushButton" name="m_button">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>370</x>
<y>120</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>=</string>
</property>
</widget>
<widget class="QLineEdit" name="m_editX">
<property name="geometry">
<rect>
<x>40</x>
<y>110</y>
<width>120</width>
<height>50</height>
</rect>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="m_editY">
<property name="geometry">
<rect>
<x>220</x>
<y>110</y>
<width>121</width>
<height>41</height>
</rect>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="m_editZ">
<property name="geometry">
<rect>
<x>490</x>
<y>110</y>
<width>121</width>
<height>41</height>
</rect>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="m_label">
<property name="geometry">
<rect>
<x>180</x>
<y>120</y>
<width>67</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>+</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
ui_Calculator2.h
/********************************************************************************
** Form generated from reading UI file 'Calcultor2.ui'
**
** Created by: Qt User Interface Compiler version 5.15.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_CALCULATOR2_H
#define UI_CALCULATOR2_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QDialog>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
QT_BEGIN_NAMESPACE
class Ui_CalculatorDialog
{
public:
QPushButton *m_button;
QLineEdit *m_editX;
QLineEdit *m_editY;
QLineEdit *m_editZ;
QLabel *m_label;
void setupUi(QDialog *CalculatorDialog)
{
if (CalculatorDialog->objectName().isEmpty())
CalculatorDialog->setObjectName(QString::fromUtf8("CalculatorDialog"));
CalculatorDialog->resize(724, 318);
QFont font;
font.setPointSize(20);
CalculatorDialog->setFont(font);
m_button = new QPushButton(CalculatorDialog);
m_button->setObjectName(QString::fromUtf8("m_button"));
m_button->setEnabled(false);
m_button->setGeometry(QRect(370, 120, 91, 31));
m_editX = new QLineEdit(CalculatorDialog);
m_editX->setObjectName(QString::fromUtf8("m_editX"));
m_editX->setGeometry(QRect(40, 110, 120, 50));
m_editX->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
m_editY = new QLineEdit(CalculatorDialog);
m_editY->setObjectName(QString::fromUtf8("m_editY"));
m_editY->setGeometry(QRect(220, 110, 121, 41));
m_editY->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
m_editZ = new QLineEdit(CalculatorDialog);
m_editZ->setObjectName(QString::fromUtf8("m_editZ"));
m_editZ->setGeometry(QRect(490, 110, 121, 41));
m_editZ->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
m_editZ->setReadOnly(true);
m_label = new QLabel(CalculatorDialog);
m_label->setObjectName(QString::fromUtf8("m_label"));
m_label->setGeometry(QRect(180, 120, 67, 17));
retranslateUi(CalculatorDialog);
QMetaObject::connectSlotsByName(CalculatorDialog);
} // setupUi
void retranslateUi(QDialog *CalculatorDialog)
{
CalculatorDialog->setWindowTitle(QCoreApplication::translate("CalculatorDialog", "\350\256\241\347\256\227\345\231\250", nullptr));
m_button->setText(QCoreApplication::translate("CalculatorDialog", "=", nullptr));
m_label->setText(QCoreApplication::translate("CalculatorDialog", "+", nullptr));
} // retranslateUi
};
namespace Ui {
class CalculatorDialog: public Ui_CalculatorDialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_CALCULATOR2_H
(1)方式一:直接继承;
CalculatorDialog.cpp
#include "CalculatorDialog.h"
//构造函数
CalculatorDialog::CalculatorDialog(void)
{
//界面初始化
setupUi(this);
setWindowTitle("计算器");
//左操作数,this即为当前父窗口指针
m_editX->setValidator(new QDoubleValidator(this));//数字验证器
//右操作数
m_editY->setValidator(new QDoubleValidator(this));//数字验证器
//信号和槽的连接
//左右操作数文本改变时,发送信号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;
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();
//double转QString
QString str = QString::number(res);
//显示字符串形式的结果
m_editZ->setText(str);
}
CalculatorDialog.h
#ifndef __CALCULATORDIALOG_H
#define __CALCALATORDIALOG_H
#include "ui_Calculator2.h"
#include <QDoubleValidator>//验证器
class CalculatorDialog:public QDialog, public Ui::CalculatorDialog{
Q_OBJECT //moc
public:
CalculatorDialog(void);
public slots:
//使能等号按钮的槽操作数
void enableButton(void);
//计算结果和显示的槽函数
void calcClicked(void);
};
#endif
main.cpp
#include<QApplication>
#include"CalculatorDialog.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
CalculatorDialog calc;
calc.show();
return app.exec();
}
(2)方式二:组合方式;
(下面只列出更改过的两个文件,其余不变)
CalculatorDialog.cpp
#include "CalculatorDialog.h"
//构造函数
CalculatorDialog::CalculatorDialog(void):ui(new Ui::CalculatorDialog)
{
//界面初始化
ui->setupUi(this);
setWindowTitle("计算器");
//左操作数,this即为当前父窗口指针
ui->m_editX->setValidator(new QDoubleValidator(this));//数字验证器
//右操作数
ui->m_editY->setValidator(new QDoubleValidator(this));//数字验证器
//信号和槽的连接
//左右操作数文本改变时,发送信号textChanged
connect(ui->m_editX, SIGNAL(textChanged(QString)), this, SLOT(enableButton(void)));
connect(ui->m_editY, SIGNAL(textChanged(QString)), this, SLOT(enableButton(void)));
//点击按钮,发送信号clicked
connect(ui->m_button, SIGNAL(clicked(void)), this, SLOT(calcClicked(void)));
}
//使能等号按钮的槽函数
void CalculatorDialog::enableButton(void)
{
bool bXOk, bYOk;
ui->m_editX->text().toDouble(&bXOk);
ui->m_editY->text().toDouble(&bYOk);
//左右操作数均有效时,使能按钮,否则禁用
ui->m_button->setEnabled(bXOk && bYOk);
}
//计算结果和显示结果的槽函数
void CalculatorDialog::calcClicked(void)
{
double res = ui->m_editX->text().toDouble() + ui->m_editY->text().toDouble();
//double转QString
QString str = QString::number(res);
//显示字符串形式的结果
ui->m_editZ->setText(str);
}
CalculatorDialog.h
#ifndef __CALCULATORDIALOG_H
#define __CALCALATORDIALOG_H
#include "ui_Calculator2.h"
#include <QDoubleValidator>//验证器
//组合方式
class CalculatorDialog:public QDialog{
Q_OBJECT //moc
public:
CalculatorDialog(void);
public slots:
//使能等号按钮的槽操作数
void enableButton(void);
//计算结果和显示的槽函数
void calcClicked(void);
private:
//将来通过"ui->"访问和界面相关的代码
Ui::CalculatorDialog* ui;
};
#endif