我是个Qt 初学者,我根据《C++ qt GUI 编程》中的那个shape changing dialog自己手写了代码来实习(书上是用Qt designer做的),构建没有错误,但是在运行时出现错误: 段错误(核心已转储)。我用的是ubuntu 12.04 和qt 4.8,代码如下:
//sortdialog.h--------------------------------------------------------------------------------------------------------------------------------------------------------------------
#ifndef SORTDIALOG_H
#define SORTDIALOG_H
#include<QtGui>
class sortDialog:public QDialog
{
public:
sortDialog(QWidget *parent=0);
void setComboRange(QChar first,QChar last);
private:
QGroupBox *group;
QGroupBox *group_2;
QGroupBox *group_3;
QComboBox *combo_11,*combo_12,*combo_21,*combo_22,*combo_31,*combo_32;
QLabel *label_11,*label_12,*label_21,*label_22,*label_31,*label_32;
QPushButton *okButton,*cancelButton,*moreButton;
QHBoxLayout *hLayout_1;
QVBoxLayout *vLayout_1;
QHBoxLayout *hLayout_2;
QVBoxLayout *vLayout_2;
QHBoxLayout *hLayout_3;
QVBoxLayout *vLayout_3;
QVBoxLayout *leftUpLayout;
QHBoxLayout *upLayout;
QVBoxLayout *rightLayout;
QVBoxLayout *mainLayout;
};
void sortDialog::setComboRange(QChar first,QChar last)
{
combo_11->clear();
combo_12->clear();
combo_21->clear();
combo_22->clear();
combo_31->clear();
combo_32->clear();
combo_11->addItem("None");
combo_12->addItem("Ascending");
combo_12->addItem("Descending");
combo_21->addItem("None");
combo_22->addItem("Ascending");
combo_22->addItem("Descending");
combo_31->addItem("None");
combo_32->addItem("Ascending");
combo_32->addItem("Descending");
QChar ch=first;
while(ch<=last)
{
combo_11->addItem(QString(ch));
combo_21->addItem(QString(ch));
combo_31->addItem(QString(ch));
ch=ch.unicode()+1;
}
}
sortDialog::sortDialog(QWidget *parent):QDialog(parent)
{
group->setTitle(tr("&Primary key"));
group_2->setTitle(tr("&Secondary key"));
group_3->setTitle(tr("&Tertiary key"));
setComboRange('A','Z');
label_11=new QLabel(tr("Column:"));
label_12=new QLabel(tr("Order:"));
label_21=new QLabel(tr("Column:"));
label_22=new QLabel(tr("Order:"));
label_31=new QLabel(tr("Column:"));
label_32=new QLabel(tr("Order:"));
okButton=new QPushButton(tr("&OK"));
cancelButton=new QPushButton(tr("&Cancel"));
moreButton=new QPushButton(tr("&More"));
hLayout_1=new QHBoxLayout;
hLayout_1->addWidget(combo_11);
hLayout_1->addStretch();
vLayout_1=new QVBoxLayout;
vLayout_1->addLayout(hLayout_1);
vLayout_1->addWidget(combo_12);
group->setLayout(vLayout_1);
hLayout_2=new QHBoxLayout;
hLayout_2->addWidget(combo_21);
hLayout_2->addStretch();
vLayout_2=new QVBoxLayout;
vLayout_2->addLayout(hLayout_2);
vLayout_2->addWidget(combo_22);
group_2->setLayout(vLayout_2);
hLayout_3=new QHBoxLayout;
hLayout_3->addWidget(combo_31);
hLayout_3->addStretch();
vLayout_3=new QVBoxLayout;
vLayout_3->addLayout(hLayout_3);
vLayout_3->addWidget(combo_32);
group_3->setLayout(vLayout_3);
rightLayout=new QVBoxLayout;
rightLayout->addWidget(okButton);
rightLayout->addWidget(cancelButton);
rightLayout->addStretch();
rightLayout->addWidget(moreButton);
leftUpLayout=new QVBoxLayout;
leftUpLayout->addWidget(group);
leftUpLayout->addStretch();
upLayout=new QHBoxLayout;
upLayout->addLayout(leftUpLayout);
upLayout->addLayout(rightLayout);
mainLayout=new QVBoxLayout;
mainLayout->addLayout(upLayout);
mainLayout->addWidget(group_2);
mainLayout->addWidget(group_3);
setLayout(mainLayout);
QObject::connect(okButton,SIGNAL(clicked()),this,SLOT(accept()));
QObject::connect(cancelButton,SIGNAL(clicked()),this,SLOT(reject()));
QObject::connect(moreButton,SIGNAL(toggled(bool)),group_2,SLOT(setVisible(bool)));
QObject::connect(moreButton,SIGNAL(toggled(bool)),group_3,SLOT(setVisible(bool)));
}
#endif
//main.cpp--------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<QApplication>
#include"sortdialog.h"
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
sortDialog s;
s.show();
return app.exec();
}
求高手指教!!!不胜感激。