我用qt学习GUI编程。我有使用变形对话框的问题。像这样:
为此,我添加了一些信号槽,如connect(ui->pbMore,SIGNAL(toggled(bool)),ui->gbSecond,SLOT(setShown(bool)));
,但它无法正常工作。我尝试setVisible(bool)
或在GUI上设置连接,但也没有运气。
这是C ++ GUI Programming with Qt 4,Second Edition一书中的一个例子。他们和我一样。那么我的错误是什么?
编辑:添加ui文件和代码。
dialogexpand.h
#ifndef DIALOGEXPAND_H
#define DIALOGEXPAND_H
#include <QDialog>
namespace Ui {
class DialogExpand;
}
class DialogExpand : public QDialog
{
Q_OBJECT
public:
explicit DialogExpand(QWidget *parent = 0);
~DialogExpand();
private:
Ui::DialogExpand *ui;
public:
void SortDialog(QWidget *parent = 0);
void setColumnRange(QChar first, QChar last);
};
dialogexpand.cpp
#include "dialogexpand.h"
#include "ui_dialogexpand.h"
DialogExpand::DialogExpand(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogExpand)
{
ui->setupUi(this);
connect(ui->pbMore,SIGNAL(toggled(bool)),ui->gSec,SLOT(setVisible(bool)));
connect(ui->pbMore,SIGNAL(toggled(bool)),ui->gbThird,SLOT(setShown(bool)));
// ui->gbThird->hide();
// ui->gSec->hide();
SortDialog();
}
DialogExpand::~DialogExpand()
{
delete ui;
}
void DialogExpand::SortDialog(QWidget *parent)
{
// ui->gSec->hide();
// ui->gbThird->hide();
layout()->setSizeConstraint(QLayout::SetFixedSize);
setColumnRange('A', 'Z');
}
void DialogExpand::setColumnRange(QChar first, QChar last)
{
ui->cbFirstColumn->clear();
ui->cbSecColumn->clear();
ui->cbSecColumn_2->clear();
ui->cbFirstColumn->addItem(tr("None"));
ui->cbSecColumn->addItem(tr("None"));
ui->cbFirstColumn->setMinimumSize(
ui->cbSecColumn->sizeHint());
QChar ch = first;
while (ch <= last) {
ui->cbFirstColumn->addItem(QString(ch));
ui->cbSecColumn->addItem(QString(ch));
ui->cbSecColumn_2->addItem(QString(ch));
ch = ch.unicode() + 1;
}
}
ui file
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DialogExpand</class>
<widget class="QDialog" name="DialogExpand">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>354</width>
<height>307</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QGroupBox" name="gbFirst">
<property name="title">
<string>&Primary key</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="lbColumn">
<property name="text">
<string>Column</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="cbFirstColumn">
<item>
<property name="text">
<string>None</string>
</property>
</item>
</widget>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>109</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lbOrder">
<property name="text">
<string>Order</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="cbSecColumn_2">
<item>
<property name="text">
<string>Ascending</string>
</property>
</item>
<item>
<property name="text">
<string>Descending</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1" rowspan="2">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pbOk">
<property name="text">
<string>&OK</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pbCancel">
<property name="text">
<string>&Cancel</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<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>13</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pbMore">
<property name="text">
<string>&More</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>59</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0">
<widget class="QGroupBox" name="gSec">
<property name="title">
<string>&Sec key</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="lbColumn_2">
<property name="text">
<string>Column</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="cbSecColumn">
<item>
<property name="text">
<string>None</string>
</property>
</item>
</widget>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lbOrder_2">
<property name="text">
<string>Order</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="cbSecOrder">
<item>
<property name="text">
<string>Ascending</string>
</property>
</item>
<item>
<property name="text">
<string>Descending</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="gbThird">
<property name="title">
<string>&Teriatriy key</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="lbColumn_3">
<property name="text">
<string>Column</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="cbThirdColumn">
<item>
<property name="text">
<string>None</string>
</property>
</item>
</widget>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lbOrder_3">
<property name="text">
<string>Order</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="cbThirdOrder">
<item>
<property name="text">
<string>Ascending</string>
</property>
</item>
<item>
<property name="text">
<string>Descending</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>pbOk</sender>
<signal>clicked()</signal>
<receiver>DialogExpand</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>304</x>
<y>22</y>
</hint>
<hint type="destinationlabel">
<x>268</x>
<y>104</y>
</hint>
</hints>
</connection>
<connection>
<sender>pbCancel</sender>
<signal>clicked()</signal>
<receiver>DialogExpand</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>324</x>
<y>54</y>
</hint>
<hint type="destinationlabel">
<x>341</x>
<y>111</y>
</hint>
</hints>
</connection>
</connections>
</ui>