Qt-spreadsheet>>Finddialog&Sortdialog

    科技2024-09-27  16

    finddialog.h

    //防止对头文件的多重包含 #ifndef _FINDDIALOG_H_ #define _FINDDIALOG_H_ //QDialog是基类 #include <QDialog> //前向声明 只能使用指针或者引用 //不用包含类定义中的所有细节 //但是分配空间时必须在finddialog.cpp中进行定义 class QCheckBox; class QLabel; class QLineEdit; class QPushButton; //QDialog是基类 继承自QWidget //QLineEdit与QDialog是同等地位 //FindDialog是QDialog的子类 class FindDialog:public QDialog { //定义了信号和槽的类 必须使用 Q_OBJECT Q_OBJECT public: //构造函数,parent为父窗口,参数默认值是一个空指针 //意味着没有父对象 FindDialog(QWidget *parent = nullptr); signals: //Qt::CaseSensitivity表示匹配的灵敏度 是否区分大小写 //signals部分表明点击Find按钮所发射的信号 //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 *caseCheckBox; QCheckBox *backwardCheckBox; QPushButton *findButton; QPushButton *closeButton; }; #endif // FINDDIALOG_H

    finddialog.cpp

    #include "finddialog.h" #include <QtWidgets> FindDialog::FindDialog(QWidget *parent) :QDialog(parent) { label = new QLabel(tr("Find &what:")); lineEdit = new QLineEdit(); //将Lineedit设置为label的伙伴 焦点。 label->setBuddy(lineEdit); caseCheckBox = new QCheckBox(tr("Match &case")); backwardCheckBox = new QCheckBox(tr("Search &backard")); findButton = new QPushButton(tr("&Find")); //成为对话框的默认按钮 按下'enter'时候能够按下对应按钮 findButton->setDefault(true); //不可编辑 findButton->setEnabled(false); closeButton = new QPushButton(tr("Close")); //由于Qobject是FindDialog的父对象 所以可以省去前缀 connect(lineEdit,SIGNAL(textChanged(const QString &)), this,SLOT(enableFindButton(const QString &))); connect(findButton,SIGNAL(click()), this,SLOT(findClicked())); connect(closeButton,SIGNAL(clicked()), this,SLOT(close())); //设置布局 //布局管理器类的精妙之处在于他们不是窗口部件 //所以能很好的区分窗口部件和布局 //当子布局对象添加到父布局对象中,能够自动重定义自己的父对象 QHBoxLayout *topLeftLayout = new QHBoxLayout; topLeftLayout->addWidget(label); topLeftLayout->addWidget(lineEdit); QVBoxLayout *leftLayout = new QVBoxLayout; //注意 是addLayout leftLayout->addLayout(topLeftLayout); leftLayout->addWidget(caseCheckBox); leftLayout->addWidget(backwardCheckBox); QVBoxLayout *rightLayout = new QVBoxLayout; rightLayout->addWidget(findButton); rightLayout->addWidget(closeButton); //stretch分隔符 rightLayout->addStretch(); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addLayout(leftLayout); mainLayout->addLayout(rightLayout); setLayout(mainLayout); setWindowTitle(tr("Find")); //sizehint返回一个理想的高度 setFixedHeight(sizeHint().height()); } //没必要写析构函数 因为删除父对象的同时会删除所有的子对象 void FindDialog::findClicked() { QString text = lineEdit->text(); Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive; if(backwardCheckBox->isChecked()) { //emit是qt关键字,会被预处理为标准的c++代码 emit findPrevious(text,cs); } else { emit findNext(text,cs); } } void FindDialog::enableFindButton(const QString &text) { //不空的话 findButton->setEnabled(!text.isEmpty()); }

    sortdialog.h

    #ifndef SORTDIALOG_H #define SORTDIALOG_H #include <QDialog> class sortdialog : public QDialog { Q_OBJECT public: sortdialog(QWidget *parent = nullptr); //Qt::WFlags flags = 0); //~sortdialog(); void setColumnRange(QChar first, QChar last); //private: }; #endif // SORTDIALOG_H

    sortdialog.cpp

    #include "sortdialog.h" #include <QtWidgets> sortdialog::sortdialog(QWidget *parent) //Qt::WFlags flags) : QDialog(parent) { setupUi(this); secondaryGroupBox->hide(); tertiaryGroupBox->hide(); layout()->setSizeConstraint(QLayout::SetFixedSize); setColumnRange('A','Z'); } //sortdialog::~sortdialog() void sortdialog::setColumnRange(QChar first, QChar last) { primaryColumnCombo->clear(); secondaryColumnCombo->clear(); tertiaryColumnCombo->clear(); secondaryColumnCombo->addItem(tr("None")); tertiaryColumnCombo->addItem(tr("None")); primaryColumnCombo->setMinimumSize(secondaryColumnCombo->sizeHint()); QChar ch = first; while (ch <= last) { primaryColumnCombo->addItem(QString(ch)); secondaryColumnCombo->addItem(QString(ch)); tertiaryColumnCombo->addItem(QString(ch)); ch = ch.unicode() + 1; } }
    Processed: 0.008, SQL: 8