Qt快速设计对话框(控件属性)-designer(1)

学习Qt中利用qt designer快速设计对话框,现在简单记录一下如何使用qt designer。
在这里插入图片描述
1.使用qt designer设计一个添加一个designer之后,qt会自动增加一个dialog.ui、dialog.h、dialog.cpp文件,ui文件是用来设计界面,而dialog.h和dialog.cpp文件是用来添加控件动作的。

2.同时,我们会发现dialog.cpp文件中会多包含一个未被加载的ui_dialog.h头文件,这个文件存放ui文件中设置界面是什么样子的实现。

3.如图设置以下五个控件
一个label,一个text edit,一个Horizontal spacer,两个button。

4.点击label,设置text属性为“&Cell Location:”,设置文本显示时,前面加‘&’符号,是为了后面设置buddy窗口部件伙伴。
快速设置窗口部件伙伴,在designer界面最上方
在这里插入图片描述
第三个就是快速设置窗口部件伙伴按钮
点击要设置的部件,向需要设置伙伴的控件
在这里插入图片描述
在这里插入图片描述
如下代表设置完成,设置完成后点击第一个框,退出快速设置窗口部件伙伴模式。

5.点击text edit,设置obeject name为lineedit

6.设置第一个按钮,object name设置为OKButton,text属性设置为OK;另外一个按钮object name属性设置为cancelButton,text属性设置未Cancel。然后我们在保存编译一下后可以发现ui_dialog.h头文件中代码发生了变化。—设置object name,实际上在编译的时候,该控件对象名就已经修改

7设置整个Dialog属性为GoToCellDialog,这个时候再进行编译是不过的,原因是因为这边的ui objectname在dialog.ui中会改变。
在这里插入图片描述
这会导致ui_dialog.h头文件中创建的dialog类名发生变化。
整体直接看一下头文件中的代码:

/********************************************************************************
** Form generated from reading UI file 'dialog.ui'
**
** Created by: Qt User Interface Compiler version 5.13.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_DIALOG_H
#define UI_DIALOG_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QDialog>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>

QT_BEGIN_NAMESPACE

class Ui_GoToCellDialog//类名已经发生改变
{
public:
    QPushButton *OKButton;
    QPushButton *cancelButton;
    QLineEdit *CelllineEdit;
    QLabel *Celllabel;

    void setupUi(QDialog *GoToCellDialog)
    {
        if (GoToCellDialog->objectName().isEmpty())
            GoToCellDialog->setObjectName(QString::fromUtf8("GoToCellDialog"));
        GoToCellDialog->resize(361, 137);
        OKButton = new QPushButton(GoToCellDialog);
        OKButton->setObjectName(QString::fromUtf8("OKButton"));//设置object name,实际上在编译的时候,该控件对象名就已经修改
        OKButton->setEnabled(true);
        OKButton->setGeometry(QRect(180, 80, 75, 23));
        OKButton->setCheckable(false);
        OKButton->setAutoDefault(false);
        cancelButton = new QPushButton(GoToCellDialog);
        cancelButton->setObjectName(QString::fromUtf8("cancelButton"));
        cancelButton->setGeometry(QRect(277, 81, 75, 23));
        CelllineEdit = new QLineEdit(GoToCellDialog);
        CelllineEdit->setObjectName(QString::fromUtf8("CelllineEdit"));
        CelllineEdit->setGeometry(QRect(99, 32, 133, 20));
        Celllabel = new QLabel(GoToCellDialog);
        Celllabel->setObjectName(QString::fromUtf8("Celllabel"));
        Celllabel->setGeometry(QRect(9, 32, 84, 16));
#if QT_CONFIG(shortcut)
        Celllabel->setBuddy(CelllineEdit);
#endif // QT_CONFIG(shortcut)

        retranslateUi(GoToCellDialog);

        OKButton->setDefault(true);


        QMetaObject::connectSlotsByName(GoToCellDialog);
    } // setupUi

    void retranslateUi(QDialog *GoToCellDialog)
    {
        GoToCellDialog->setWindowTitle(QCoreApplication::translate("GoToCellDialog", "Go to Cell", nullptr));
        OKButton->setText(QCoreApplication::translate("GoToCellDialog", "OK", nullptr));
        cancelButton->setText(QCoreApplication::translate("GoToCellDialog", "Cancel", nullptr));
        Celllabel->setText(QCoreApplication::translate("GoToCellDialog", "&Cell Location:", nullptr));
    } // retranslateUi

};

namespace Ui {
    class GoToCellDialog: public Ui_GoToCellDialog {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_DIALOG_H

所以在这种情况下,需要修改dialog文件中使用到的代码:
原文件中

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = nullptr);
    ~Dialog();

private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H

修改成为:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class GoToCellDialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = nullptr);
    ~Dialog();

private:
    Ui::GoToCellDialog *ui;
};

#endif // DIALOG_H


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值