Qt实现扩展对话框

4 篇文章 0 订阅

最近在看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>&amp;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>&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;/head&gt;&lt;body style=&quot; white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;&quot;&gt;&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Order:&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
        </property>
       </widget>
      </item>
      <item row="0" column="0">
       <widget class="QLabel" name="primaryColumnLabel">
        <property name="text">
         <string>&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;/head&gt;&lt;body style=&quot; white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;&quot;&gt;&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Column:&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</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>&amp;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>&amp;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>&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;/head&gt;&lt;body style=&quot; white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;&quot;&gt;&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Order:&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
        </property>
       </widget>
      </item>
      <item row="0" column="0">
       <widget class="QLabel" name="secondaryColumnLabel">
        <property name="text">
         <string>&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;/head&gt;&lt;body style=&quot; white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;&quot;&gt;&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Column:&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</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>&amp;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>&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;/head&gt;&lt;body style=&quot; white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;&quot;&gt;&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Order:&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
        </property>
       </widget>
      </item>
      <item row="0" column="0">
       <widget class="QLabel" name="tertiaryColumnLabel">
        <property name="text">
         <string>&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;/head&gt;&lt;body style=&quot; white-space: pre-wrap; font-family:Sans Serif; font-weight:400; font-style:normal; text-decoration:none;&quot;&gt;&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Column:&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值