Qt : 创建对话框(1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39568531/article/details/80038920

一个简单的FindDialog掌握基本的布局器和组合布局器的使用。


#ifndef FDIALOG_H
#define FDIALOG_H

#include <QDialog>
#include"QPushButton"
#include"QLineEdit"
#include"QLabel"
#include"QCheckBox"

class FDialog : public QDialog
{
    Q_OBJECT

public:
    FDialog(QWidget *parent = 0);
    ~FDialog();

signals:
    void findnext(const QString& str,Qt::CaseSensitivity cs);
    void findprevious(const QString& str,Qt::CaseSensitivity cs);

public slots:
    void on_findbtnclicked();
    void on_enablefindbtn(const QString& text);

public:
    QLabel* label;
    QLineEdit* lineEdt;
    QCheckBox* caseCheckBox;
    QCheckBox* backwardCheckBox;
    QPushButton* findbtn;
    QPushButton* closebtn;
};

#endif // FDIALOG_H
#include "fdialog.h"
#include"QString"
#include"QHBoxLayout"
#include"QVBoxLayout"
#include"QLayout"
#include"QGridLayout"


FDialog::FDialog(QWidget *parent)
    : QDialog(parent)
{

    label = new QLabel(tr("Find &What"));
    lineEdt = new QLineEdit;
    label->setBuddy(lineEdt);

    caseCheckBox = new QCheckBox(tr("Match &case"));
    backwardCheckBox = new QCheckBox(tr("search &backward"));
    findbtn = new QPushButton(tr("&Find"));
    closebtn = new QPushButton(tr("Close"));

    findbtn->setDefault(true);
    findbtn->setEnabled(false);

    connect(lineEdt,SIGNAL(textChanged(const QString&)),this,SLOT(on_enablefindbtn(QString)));
    connect(findbtn,SIGNAL(clicked(bool)),this,SLOT(on_findbtnclicked()));
    connect(closebtn,SIGNAL(clicked(bool)),this,SLOT(close()));


    QHBoxLayout* topleftlayout = new QHBoxLayout;
    topleftlayout->addWidget(label);
    topleftlayout->addWidget(lineEdt);

    QVBoxLayout* leftlayout = new QVBoxLayout;
    leftlayout->addLayout(topleftlayout);
    leftlayout->addWidget(caseCheckBox);
    leftlayout->addWidget(backwardCheckBox);

    QVBoxLayout* rightlayout = new QVBoxLayout;
    rightlayout->addWidget(findbtn);
    rightlayout->addWidget(closebtn);
    rightlayout->addStretch();

    QHBoxLayout* mainlayout = new QHBoxLayout;
    mainlayout->addLayout(leftlayout);
    mainlayout->addLayout(rightlayout);
    this->setLayout(mainlayout);


    this->setWindowTitle(tr("Find"));
    setFixedHeight(sizeHint().height());//设置长宽都固定
    setFixedWidth(sizeHint().width());
}

FDialog::~FDialog()
{

}


void FDialog::on_findbtnclicked()
{
    QString text = lineEdt->text();
    Qt::CaseSensitivity cs = caseCheckBox->isChecked() ?Qt::CaseSensitive:Qt::CaseInsensitive;//判断checkbox按钮是否被点击
    if(backwardCheckBox->isChecked())
    {
        emit findprevious(text,cs);
    }
    else{
        emit findnext(text,cs);
    }

}
void FDialog::on_enablefindbtn(const QString& text)
{
    findbtn->setEnabled(!text.isEmpty());//如果为输入不可被点击
    update();
}



猜你喜欢

转载自blog.csdn.net/weixin_39568531/article/details/80038920
今日推荐