最近在看C++ GUI Qt编程这个书,按照书中第2章的描述,实现了一个扩展对话框。
未展开时的界面:
展开后的界面:
实现的源码如下:
#ifndef __SORTDIALOG_H__
#define __SORTDIALOG_H__
#include <QDialog>
#include "ui_SortDialog.h"
class SortDialog : public QDialog, public Ui::SortDialog
{
Q_OBJECT
protected slots:
//void on_moreButton_clicked();
public:
SortDialog(QWidget *parent = NULL);
void setColumnRange(QChar first, QChar last);
};
#endif
#include "SortDialog.h"
/*void SortDialog::on_moreButton_clicked()
{
static bool flag = true;
if(flag)
{
secondaryGroupBox->show();
tertiaryGroupBox->show();
resize(322, 300);
}
else
{
secondaryGroupBox->hide();
tertiaryGroupBox->hide();
resize(322, 120);
}
flag = !flag;
}*/
SortDialog::SortDialog(QWidget *parent) : QDialog(parent)
{
setupUi(this);
secondaryGroupBox->hide();
tertiaryGroupBox->hide();
layout()->setSizeConstraint(QLayout::SetFixedSize);
setColumnRange('A', 'Z');
}
void SortDialog::setColumnRange(QChar first, QChar last)
{
primaryColumnCombo->clear();
secondaryColumnCombo->clear();
tertiaryColumnCombo->clear();
primaryColumnCombo->addItem("None");
secondaryColumnCombo->addItem("None");
tertiaryColumnCombo->addItem("None");
primaryColumnCombo->setMinimumSize(secondaryColumnCombo->sizeHint());
while(first <= last)
{
primaryColumnCombo->addItem(first);
secondaryColumnCombo->addItem(first);
tertiaryColumnCombo->addItem(first);
first = first.unicode() + 1;
}
}
界面文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SortDialog</class>
<widget class="QDialog" name="SortDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>256</width>
<height>275</height>
</rect>
</property>
<property name="windowTitle">
<string>Sort</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QGroupBox" name="primaryGroupBox">
<property name="title">
<string>&Primary Key</string>
</property>
<layout class="QGridLayout">
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="primaryOrderCombo">
<item>
<property name="text">
<string>Ascending</string>
</property>
</item>
<item>
<property name="text">
<string>Descending</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="primaryOrderLabel">
<property name="text">
<string><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Order:</p></body></html></string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="primaryColumnLabel">
<property name="text">
<string><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Column:</p></body></html></string>
</property>
</widget>
</item>
<item row="0" column="2">
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>31</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="primaryColumnCombo">
<item>
<property name="text">
<string>None</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1" rowspan="2">
<layout class="QVBoxLayout">
<item>
<widget class="QPushButton" name="okButton">
<property name="text">
<string>OK</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cancelButton">
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="moreButton">
<property name="text">
<string>&More</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0">
<widget class="QGroupBox" name="secondaryGroupBox">
<property name="title">
<string>&Secondary Key</string>
</property>
<layout class="QGridLayout">
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="secondaryOrderCombo">
<item>
<property name="text">
<string>Ascending</string>
</property>
</item>
<item>
<property name="text">
<string>Descending</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="secondaryOrderLabel">
<property name="text">
<string><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Order:</p></body></html></string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="secondaryColumnLabel">
<property name="text">
<string><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Column:</p></body></html></string>
</property>
</widget>
</item>
<item row="0" column="2">
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>31</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="secondaryColumnCombo">
<item>
<property name="text">
<string>None</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="tertiaryGroupBox">
<property name="title">
<string>&Tertiary Key</string>
</property>
<layout class="QGridLayout">
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="tertiaryOrderCombo">
<item>
<property name="text">
<string>Ascending</string>
</property>
</item>
<item>
<property name="text">
<string>Descending</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="tertiaryOrderLabel">
<property name="text">
<string><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Order:</p></body></html></string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="tertiaryColumnLabel">
<property name="text">
<string><html><head><meta name="qrichtext" content="1" /></head><body style=" white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Column:</p></body></html></string>
</property>
</widget>
</item>
<item row="0" column="2">
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>31</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="tertiaryColumnCombo">
<item>
<property name="text">
<string>None</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<tabstops>
<tabstop>primaryColumnCombo</tabstop>
<tabstop>primaryOrderCombo</tabstop>
<tabstop>secondaryColumnCombo</tabstop>
<tabstop>secondaryOrderCombo</tabstop>
<tabstop>tertiaryColumnCombo</tabstop>
<tabstop>tertiaryOrderCombo</tabstop>
<tabstop>okButton</tabstop>
<tabstop>cancelButton</tabstop>
<tabstop>moreButton</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>moreButton</sender>
<signal>toggled(bool)</signal>
<receiver>secondaryGroupBox</receiver>
<slot>setVisible(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>288</x>
<y>141</y>
</hint>
<hint type="destinationlabel">
<x>104</x>
<y>166</y>
</hint>
</hints>
</connection>
<connection>
<sender>moreButton</sender>
<signal>toggled(bool)</signal>
<receiver>tertiaryGroupBox</receiver>
<slot>setVisible(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>240</x>
<y>141</y>
</hint>
<hint type="destinationlabel">
<x>170</x>
<y>307</y>
</hint>
</hints>
</connection>
<connection>
<sender>okButton</sender>
<signal>clicked()</signal>
<receiver>SortDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>257</x>
<y>25</y>
</hint>
<hint type="destinationlabel">
<x>283</x>
<y>268</y>
</hint>
</hints>
</connection>
<connection>
<sender>cancelButton</sender>
<signal>clicked()</signal>
<receiver>SortDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>225</x>
<y>56</y>
</hint>
<hint type="destinationlabel">
<x>264</x>
<y>335</y>
</hint>
</hints>
</connection>
</connections>
</ui>