「Qt Widget中文示例指南」如何实现行编辑功能

Qt 是目前最先进、最完整的跨平台C++开发工具。它不仅完全实现了一次编写,所有平台无差别运行,更提供了几乎所有开发过程中需要用到的工具。如今,Qt已被运用于超过70个行业、数千家企业,支持数百万设备及应用。

Line Edits(行编辑)示例演示了QLineEdit的多种使用方式,并显示了各种属性和验证器对用户提供的输入和输出的影响。

点击获取Qt Widget组件下载(Q技术交流:166830288)

「Qt Widget中文示例指南」如何实现一个分组框

该示例由单个Window类组成,其中包含具有不同输入约束和显示属性的行编辑选择,这些属性可以通过从组合框中选择项来更改。将它们放在一起可以帮助开发人员选择合适的属性用于行编辑,并且可以很容易地比较每个验证器对用户输入的影响。

Window类定义

Window类继承了QWidget并包含一个构造函数和几个槽:

class Window : public QWidget
{
Q_OBJECT

public:
Window(QWidget *parent = nullptr);

public slots:
void echoChanged(int);
void validatorChanged(int);
void alignmentChanged(int);
void inputMaskChanged(int);
void accessChanged(int);

private:
QLineEdit *echoLineEdit;
QLineEdit *validatorLineEdit;
QLineEdit *alignmentLineEdit;
QLineEdit *inputMaskLineEdit;
QLineEdit *accessLineEdit;
};

当在关联的组合框中选择了新的验证器时,这些槽用于更新给定行编辑的验证器类型,行编辑保存在窗口中,以便在这些槽中使用。

Window类实现

Window构造函数用于设置行编辑器、验证器和组合框,将来自组合框的信号连接到Window类中的槽,并在布局中安排子部件。

我们首先构建一个组框来保存标签、组合框和行编辑器,这样就可以演示QLineEdit::echoMode属性:

Window::Window(QWidget *parent)
: QWidget(parent)
{
QGroupBox *echoGroup = new QGroupBox(tr("Echo"));

QLabel *echoLabel = new QLabel(tr("Mode:"));
QComboBox *echoComboBox = new QComboBox;
echoComboBox->addItem(tr("Normal"));
echoComboBox->addItem(tr("Password"));
echoComboBox->addItem(tr("PasswordEchoOnEdit"));
echoComboBox->addItem(tr("No Echo"));

echoLineEdit = new QLineEdit;
echoLineEdit->setPlaceholderText("Placeholder Text");
echoLineEdit->setFocus();

在这一点上,这些小部件都没有被安排在布局中。最后echoLabel、echoComboBox和echoLineEdit将被放置在echoGroup组框内的垂直布局中。

类似地,我们构造组框和小部件集合来显示QIntValidator 和QDoubleValidator对行编辑器内容的影响:

QGroupBox *validatorGroup = new QGroupBox(tr("Validator"));

QLabel *validatorLabel = new QLabel(tr("Type:"));
QComboBox *validatorComboBox = new QComboBox;
validatorComboBox->addItem(tr("No validator"));
validatorComboBox->addItem(tr("Integer validator"));
validatorComboBox->addItem(tr("Double validator"));

validatorLineEdit = new QLineEdit;
validatorLineEdit->setPlaceholderText("Placeholder Text");

文本对齐由另一组小部件演示:

QGroupBox *alignmentGroup = new QGroupBox(tr("Alignment"));

QLabel *alignmentLabel = new QLabel(tr("Type:"));
QComboBox *alignmentComboBox = new QComboBox;
alignmentComboBox->addItem(tr("Left"));
alignmentComboBox->addItem(tr("Centered"));
alignmentComboBox->addItem(tr("Right"));

alignmentLineEdit = new QLineEdit;
alignmentLineEdit->setPlaceholderText("Placeholder Text");

QLineEdit 支持使用输入掩码,它们只允许用户在行编辑中输入遵循简单规范的字符,我们构建了一组小部件来演示预定义掩码的选择:

QGroupBox *inputMaskGroup = new QGroupBox(tr("Input mask"));

QLabel *inputMaskLabel = new QLabel(tr("Type:"));
QComboBox *inputMaskComboBox = new QComboBox;
inputMaskComboBox->addItem(tr("No mask"));
inputMaskComboBox->addItem(tr("Phone number"));
inputMaskComboBox->addItem(tr("ISO date"));
inputMaskComboBox->addItem(tr("License key"));

inputMaskLineEdit = new QLineEdit;
inputMaskLineEdit->setPlaceholderText("Placeholder Text");

QLineEdit的另一个有用特性是使其内容只读的能力,此属性用于控制对以下小部件组中的行编辑访问:

QGroupBox *accessGroup = new QGroupBox(tr("Access"));

QLabel *accessLabel = new QLabel(tr("Read-only:"));
QComboBox *accessComboBox = new QComboBox;
accessComboBox->addItem(tr("False"));
accessComboBox->addItem(tr("True"));

accessLineEdit = new QLineEdit;
accessLineEdit->setPlaceholderText("Placeholder Text");

现在所有的子部件都已经构造好了,我们将来自组合框的信号连接到Window对象中的槽:

connect(echoComboBox, &QComboBox::activated,
this, &Window::echoChanged);
connect(validatorComboBox, &QComboBox::activated,
this, &Window::validatorChanged);
connect(alignmentComboBox, &QComboBox::activated,
this, &Window::alignmentChanged);
connect(inputMaskComboBox, &QComboBox::activated,
this, &Window::inputMaskChanged);
connect(accessComboBox, &QComboBox::activated,
this, &Window::accessChanged);

这些连接中的每一个都使用QComboBox::activated()信号,该信号向插槽提供一个整数,这将用于有效地更改每个槽中的适当行编辑。

我们将每个组合框、行编辑和标签放置在每个组框的布局中,从echoGroup组框的布局开始:

QGridLayout *echoLayout = new QGridLayout;
echoLayout->addWidget(echoLabel, 0, 0);
echoLayout->addWidget(echoComboBox, 0, 1);
echoLayout->addWidget(echoLineEdit, 1, 0, 1, 2);
echoGroup->setLayout(echoLayout);

其他布局的构造方式相同:

QGridLayout *validatorLayout = new QGridLayout;
validatorLayout->addWidget(validatorLabel, 0, 0);
validatorLayout->addWidget(validatorComboBox, 0, 1);
validatorLayout->addWidget(validatorLineEdit, 1, 0, 1, 2);
validatorGroup->setLayout(validatorLayout);

QGridLayout *alignmentLayout = new QGridLayout;
alignmentLayout->addWidget(alignmentLabel, 0, 0);
alignmentLayout->addWidget(alignmentComboBox, 0, 1);
alignmentLayout->addWidget(alignmentLineEdit, 1, 0, 1, 2);
alignmentGroup-> setLayout(alignmentLayout);

QGridLayout *inputMaskLayout = new QGridLayout;
inputMaskLayout->addWidget(inputMaskLabel, 0, 0);
inputMaskLayout->addWidget(inputMaskComboBox, 0, 1);
inputMaskLayout->addWidget(inputMaskLineEdit, 1, 0, 1, 2);
inputMaskGroup->setLayout(inputMaskLayout);

QGridLayout *accessLayout = new QGridLayout;
accessLayout->addWidget(accessLabel, 0, 0);
accessLayout->addWidget(accessComboBox, 0, 1);
accessLayout->addWidget(accessLineEdit, 1, 0, 1, 2);
accessGroup->setLayout(accessLayout);

最后我们将每个组框放置在Window对象的网格布局中,并设置窗口标题:

QGridLayout *layout = new QGridLayout;
layout->addWidget(echoGroup, 0, 0);
layout->addWidget(validatorGroup, 1, 0);
layout->addWidget(alignmentGroup, 2, 0);
layout->addWidget(inputMaskGroup, 0, 1);
layout->addWidget(accessGroup, 1, 1);
setLayout(layout);

setWindowTitle(tr("Line Edits"));
}

槽响应用户更改组合框时发出的信号。

当Echo分组框的组合框被改变时,echoChanged()槽被调用:

void Window::echoChanged(int index)
{
switch (index) {
case 0:
echoLineEdit->setEchoMode(QLineEdit::Normal);
break;
case 1:
echoLineEdit->setEchoMode(QLineEdit::Password);
break;
case 2:
echoLineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);
break;
case 3:
echoLineEdit->setEchoMode(QLineEdit::NoEcho);
break;
}
}

槽更新同一分组框中的行编辑,来使用与分组合框中描述的条目对应的回显模式。

当Validator分组框的组合框被改变时,validatorChanged()槽被调用:

void Window::validatorChanged(int index)
{
switch (index) {
case 0:
validatorLineEdit->setValidator(nullptr);
break;
case 1:
validatorLineEdit->setValidator(new QIntValidator(
validatorLineEdit));
break;
case 2:
validatorLineEdit->setValidator(new QDoubleValidator(-999.0,
999.0, 2, validatorLineEdit));
break;
}

validatorLineEdit->clear();
}

槽或者为要使用的行编辑创建一个新的验证器,或者通过调用带有零指针的QLineEdit::setValidator()来删除正在使用的验证器。在本例中,我们清除了行编辑,以确保最初为新的验证器提供了有效的输入。

当对齐分组框的组合框被改变时,alignmentChanged()槽被调用:

void Window::alignmentChanged(int index)
{
switch (index) {
case 0:
alignmentLineEdit->setAlignment(Qt::AlignLeft);
break;
case 1:
alignmentLineEdit->setAlignment(Qt::AlignCenter);
break;
case 2:
alignmentLineEdit->setAlignment(Qt::AlignRight);
break;
}
}

这会改变文本在行编辑器中的显示方式,使其与组合框中选择的描述相对应。

inputMaskChanged()插槽处理输入掩码分组框中组合框的更改:

void Window::inputMaskChanged(int index)
{
switch (index) {
case 0:
inputMaskLineEdit->setInputMask("");
break;
case 1:
inputMaskLineEdit->setInputMask("+99 99 99 99 99;_");
break;
case 2:
inputMaskLineEdit->setInputMask("0000-00-00");
inputMaskLineEdit->setText("00000000");
inputMaskLineEdit->setCursorPosition(0);
break;
case 3:
inputMaskLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");
break;
}
}

相关组合框中的每个条目都与一个输入掩码相关联,我们通过使用合适的字符串调用QLineEdit::setInputMask()函数来设置一个新的掩码;如果使用空字符串,则禁用掩码。

accessChanged()槽处理Access分组框中组合框的更改:

void Window::accessChanged(int index)
{
switch (index) {
case 0:
accessLineEdit->setReadOnly(false);
break;
case 1:
accessLineEdit->setReadOnly(true);
break;
}
}

这里,我们简单地将分组合框中的False和True条目与传递给QLineEdit::setReadOnly()的False和True值关联起来,这允许用户启用和禁用对行编辑的输入。

Qt Widget组件推荐
  • QtitanRibbon - Ribbon UI组件:是一款遵循Microsoft Ribbon UI Paradigm for Qt技术的Ribbon UI组件,QtitanRibbon致力于为Windows、Linux和Mac OS X提供功能完整的Ribbon组件。
  • QtitanChart - Qt类图表组件:是一个C ++库,代表一组控件,这些控件使您可以快速地为应用程序提供漂亮而丰富的图表。
  • QtitanDataGrid - Qt网格组件:提供了一套完整的标准 QTableView 函数和传统组件无法实现的独特功能。使您能够将不同来源的各类数据加载到一个快速、灵活且功能强大的可编辑网格中,支持排序、分组、报告、创建带状列、拖放按钮和许多其他方便的功能。
  • QtitanDocking:允许您像 Visual Studio 一样为您的伟大应用程序配备可停靠面板和可停靠工具栏。黑色、白色、蓝色调色板完全支持 Visual Studio 2019 主题!
  • 22
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要实现Qt Widget中添加控件,可以按照以下步骤进操作。 1. 首先,在Qt项目中打开你想要添加控件Widget文件,一般为.ui文件。 2. 在Qt Designer中选择你想要添加控件的位置,比如一个按钮。 3. 在Qt Designer的左侧工具栏中,选择你要添加的控件的类型,比如QLineEdit(文本输入框)。 4. 将该控件拖拽到你在第二步中选择的位置。 5. 可以通过右键单击控件,在弹出的选项中选择“布局”来调整控件的位置和大小。 6. 在Qt Widget的头文件(.h文件)中,添加一个成员变量来引用新添加的控件,以便在代码中访问并操作它。 7. 在Qt Widget的源文件(.cpp文件)中,找到Widget的构造函数(或其他初始化函数),在其中实例化新添加的控件。 ```cpp QLineEdit *lineEdit = new QLineEdit(this); // 创建一个新的QLineEdit控件 // 可以设置控件的一些属性,比如位置和大小 lineEdit->setGeometry(100, 100, 200, 30); // 添加控件事件的监听器,比如按下按钮后的为 connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(onTextChanged(const QString &))); ``` 8. 在Qt Widget的源文件中,实现你在第7步中连接的槽函数。这将定义控件与其他部分的交互逻辑。 ```cpp void Widget::onTextChanged(const QString &text) { // 在文本改变时执的代码 qDebug() << "Text changed: " << text; } ``` 通过这些步骤,你就可以在Qt Widget中添加控件实现相应的功能了。当你运应用程序时,新添加的控件将出现在GUI中,并响应用户的操作。 ### 回答2: Qt是一个跨平台的C++应用程序开发框架,可以用于开发各种GUI界面程序。在Qt中,可以使用控件来构建用户界面,如按钮、文本框、标签等。 要在Qt Widget中添加一个控件,需要进以下步骤: 1. 创建一个新的Qt Widget项目,打开Qt Creator软件并选择新建项目。 2. 在项目向导中选择Qt Widgets Application,并输入项目名称和存储位置。 3. 在项目构建界面中选择默认配置,然后点击下一步。 4. 在类创建界面中,选择基类为QWidget,并输入类名称和包含的头文件的名称,然后点击下一步。 5. 在界面设计器中,可以看到一个空白的QWidget,我们可以在此基础上添加控件。 6. 在设计器中,选择左侧的控件库,如QToolBox,然后选择要添加的控件,如按钮。 7. 在窗口上单击并拖动鼠标来创建按钮的大小。 8. 可以在属性编辑器中修改控件的属性,如按钮的文本、大小、位置等。 9. 为按钮添加信号和槽函数,以便在按钮被点击时执相应的操作。 10. 保存并编译项目,然后运程序,即可在窗口上看到添加的控件。 以上是一个简单的Qt Widget添加控件的步骤,通过使用Qt Creator提供的界面设计器和属性编辑器,可以方便地添加和配置各种控件来构建用户界面。在实际的应用程序开发中,可以根据需要添加不同类型的控件,并通过信号和槽机制来实现控件之间的交互和响应。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值