QT-创建对话框

#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include<QDialog>

//前置声明会告诉C++编译程序类的存在,而不用提供类定义中的所有细节。
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

//FindDialog 继承 QDialog(对话框)类
class FindDialog : public QDialog{
        Q_OBJECT//对于所有定义了信号和槽的类,在类定义开始处的Q_OBJECT宏都是必需的。
public:
    //构造函数  参数为:QWidget部件类
    FindDialog(QWidget *parent = 0);

    //signals部分声明了当用户点击Find按钮时对话框所发射的两个信号
signals:
    //Qt::CaseSensitivity为枚举型,可取值Qt::CaseSensitive和Qt::CaseInsensitive
    //表示匹配的灵敏度
    void findNext(const QString &str,Qt::CaseSensitivity cs);
    void findPrevious(const QString &str,Qt::CaseSensitivity cs);

private slots:
    void findClicked();
    void enableFindButton(const QString &text);

private:
    //因为都是指针,所以可以使用前置类型。
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *backwardCheckBox;
    QCheckBox *caseCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;

};
#endif // FINDDIALOG_H

上面是头文件

finddialog.h
下面是
finddialog.cpp
#include<QPushButton>
#include<QLabel>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QCheckBox>
#include<QLineEdit>
#include"finddialog.h"

FindDialog::FindDialog(QWidget *parent)
    :QDialog(parent)
{
    //创建了一个带有快捷键ALT+W的标签
    label = new QLabel(tr("Find &what:"));

    lineEdit = new QLineEdit;

    //设置编辑器作为标签的伙伴。按下标签的快捷键时,lineEdit会接受焦点。
    label->setBuddy(lineEdit);

    caseCheckBox = new QCheckBox(tr("Match &case"));

    backwardCheckBox = new QCheckBox(tr("Search &backward"));

    //Alt+F快捷键会激活Find按钮
    findButton = new QPushButton(tr("&Find"));

    //将Find按钮作为对话框的默认按钮。默认按钮就是当用户按下Enter键时能够按下对应的按钮
    findButton->setDefault(true);
    //将Find按钮禁止
    findButton->setEnabled(false);

    closeButton = new QPushButton(tr("Close"));

    //当lineEdit里面的内容发生变化时,会调用本类的enableFindButton函数。
    QObject::connect(lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(enableFindButton(const QString &)));

    //当点击Find按钮时,会调用本类的findClicked方法.
    QObject::connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));

    //当点击Close按钮时,会调用本类或者父类的close方法
    QObject::connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));

    //水平布局  加入部件label  lineEdit
    QHBoxLayout *topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);

    //垂直布局  加入水平布局里所有的控件,接着加入caseCheckBox
    QVBoxLayout *leftLayout = new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);

    //垂直布局 加入两个按钮部件
    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    //实现布局器的空白控件分配
    rightLayout->addStretch();

    //水平布局 加入之前布局的所有控件
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
    //实现该控件的布局
    setLayout(mainLayout);

    setWindowTitle("Find");
    setFixedHeight(sizeHint().height());

}
void FindDialog::findClicked(){

        QString text = lineEdit->text();
        Qt::CaseSensitivity cs =
                  caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;

        if(backwardCheckBox->isChecked()){
            emit findPrevious(text,cs);

        }else{
            emit findNext(text,cs);
        }

}
void FindDialog::enableFindButton(const QString &text){
    findButton->setEnabled(!text.isEmpty());
}

main文件

1 #include "mainwindow.h"
2 #include <QApplication>
3 #include"finddialog.h"
4 int main(int argc,char *argv[]){
5     QApplication app(argc,argv);
6     FindDialog *dialog = new FindDialog;
7     dialog->show();
8     return app.exec();
9 }

 

转载于:https://www.cnblogs.com/teng-IT/p/6003267.html

### 如何在 Qt创建并显示临时对话框Qt 中,可以通过 `QDialog` 类或其派生类来创建临时对话框。以下是创建和显示临时对话框的详细说明: #### 创建对话框的基本步骤 1. **继承 `QDialog`**:如果需要自定义对话框,可以继承 `QDialog` 并添加所需的控件。 2. **设置模态或非模态行为**:通过调用 `exec()` 方法实现模态对话框[^3],或者使用 `show()` 方法实现非模态对话框[^2]。 3. **显示对话框**:根据需求选择合适的显示方式。 #### 示例代码 以下是一个简单的示例,展示如何创建并显示一个临时对话框: ```python from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QPushButton class TemporaryDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("临时对话框") self.setGeometry(100, 100, 300, 200) # 创建布局和控件 layout = QVBoxLayout() label = QLabel("这是一个临时对话框", self) button = QPushButton("关闭", self) button.clicked.connect(self.accept) # 点击按钮后关闭对话框 # 添加到布局中 layout.addWidget(label) layout.addWidget(button) self.setLayout(layout) # 显示对话框 app = QApplication([]) dialog = TemporaryDialog() dialog.exec_() # 使用 exec() 显示模态对话框 ``` #### 代码解释 - **继承 `QDialog`**:`TemporaryDialog` 类继承自 `QDialog`,允许用户自定义对话框的内容和行为。 - **设置窗口标题和大小**:通过 `setWindowTitle` 和 `setGeometry` 方法设置对话框的标题和初始位置及大小。 - **添加控件**:通过 `QVBoxLayout` 将标签和按钮添加到对话框中。 - **连接信号与槽**:将按钮的 `clicked` 信号连接到 `accept` 槽函数,以便点击按钮后关闭对话框- **显示对话框**:通过 `exec_()` 方法显示模态对话框。 #### 内置对话框的使用 除了自定义对话框外,Qt 还提供了多种内置对话框,例如文件对话框 (`QFileDialog`)、颜色对话框 (`QColorDialog`) 等。这些对话框可以直接调用而无需额外定义界面[^2]。例如: ```python from PyQt5.QtWidgets import QApplication, QColorDialog app = QApplication([]) # 打开颜色对话框 color = QColorDialog.getColor() if color.isValid(): print(f"选择的颜色是: {color.name()}") ``` 此代码片段展示了如何使用 `QColorDialog` 来打开一个颜色选择对话框,并获取用户选择的颜色。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值