C++ GUI Qt4编程(06)-2.3sort

1. 使用Qt设计师创建Sort对话框。

2. sortdialog.cpp

/**/
#include "sortdialog.h"

SortDialog::SortDialog(QWidget *parent)
    : QDialog(parent)
{
    setupUi(this);
    
    secondaryGroupBox->hide();        /*隐藏第二个群组框*/
    tertiaryGroupBox->hide();        /*隐藏第三个群组框*/
    /*固定对话框窗体的大小,布局负责决定对话框的大小。*/
    layout()->setSizeConstraint(QLayout::SetFixedSize);
    
    setColumnRange('A', 'Z');
    
    /*QPushButton选中checkable属性后,按一下按钮会触发一次toggled信号*/
    connect(moreButton, SIGNAL(toggled(bool)), this, SLOT(moreButtonSlot(bool)));
}

void SortDialog::setColumnRange(QChar first, QChar last)
{
    primaryColumnComboBox->clear();
    secondaryColumnComboBox->clear();
    tertiaryColumnComboBox->clear();
    
    secondaryColumnComboBox->addItem(tr("None"));
    tertiaryColumnComboBox->addItem(tr("None"));
    /*QWidget::sizeHint()函数,返回布局系统试图认同的理想的大小*/
    /*把主键组合框的最小大小设置成第二键组合框的理想大小*/
    primaryColumnComboBox->setMinimumSize(
                    secondaryColumnComboBox->sizeHint());
    

    QChar ch = first;
    while (ch <= last)
    {
        primaryColumnComboBox->addItem(QString(ch));
        secondaryColumnComboBox->addItem(QString(ch));
        tertiaryColumnComboBox->addItem(QString(ch));
        ch = ch.unicode() + 1;
    }
}

void SortDialog::moreButtonSlot(bool flag)
{
    if (flag)
    {
        moreButton->setText(tr("Advanced<<<"));
    }
    else
    {
        moreButton->setText(tr("Advanced>>>"));
    }
}

3. sortdialog.h

#ifndef SORTDIALOG_H
#define SORTDIALOG_H

#include <QDialog>
#include "ui_sortdialog.h"

class SortDialog : public QDialog, public Ui::SortDialog
{
    Q_OBJECT
    
public:
    SortDialog(QWidget *parent = 0);
    void setColumnRange(QChar first, QChar last);
public slots:
    void moreButtonSlot(bool flag);
};

#endif    /*SORTDIALOG_H*/

4. main.cpp

#include <QApplication>

#include "sortdialog.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    SortDialog *dialog = new SortDialog;
    dialog->setColumnRange('C', 'F');
    dialog->show();
    
    return app.exec();
}

posted @ 2017-06-27 19:33  seifguo  阅读(299)  评论(0编辑  收藏  举报