Qt探索之旅(三)可切换界面
程序员文章站
2022-05-28 11:52:35
...
一开始出现了这个错误,折磨了我一天,后来上网查资料,有的说是:注意布局如果网格布局的控件new成垂直布局那就出现下面的情况了https://my.oschina.net/u/1446371/blog/525494;还有的说是需要cmd一下:https://jingyan.baidu.com/article/00a07f38a31d5e82d028dc04.html
我当时采用的第二种方法,按照第二种方法做了之后没有按他说的等一会再运行Qt,而是直接就运行了Qt,结果依旧异常终止,气的我打了两把王者荣耀压压惊,唉,再运行一次竟然成了,接着运行了无数次,都成了!!!
config头文件
#ifndef CONFIG_H
#define CONFIG_H
#include <QtGui>//包含Gui中所有头文件,好处是不用一个个添加头文件,坏处是编译慢
#include "MyPage.h"
/*
#include <QtGui/QWidget>
#include <QListWidget>
#include <QListWidgetItem>//列表框添加项目
#include <QVBoxLayout>
#include <QTextCodec>
#include <QPushButton>
*/
class config : public QWidget
{
Q_OBJECT
public:
config(QWidget *parent = 0);
~config();
QListWidget *list;
MyPage1 *page1;
MyPage2 *page2;
MyPage3 *page3;
QStackedWidget *InfoWidget;
protected slots:
void pagechanged(int);
};
#endif // CONFIG_H
MyPage.h头文件
#ifndef MYPAGE_H
#define MYPAGE_H
#include <QtGui/QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
class MyPage1 : public QWidget
{
Q_OBJECT
public:
MyPage1(QWidget *parent = 0);
~MyPage1(){};
QLabel *ThemeLabel, *SeasonLabel, *LocateLabel, *VerseLabel;
QLineEdit *ThemeEdit, *SeasonEdit, *LocateEdit, *VerseEdit;
};
class MyPage2 : public QWidget
{
Q_OBJECT
public:
MyPage2(QWidget *parent = 0);
~MyPage2(){};
QLabel *ThemeLabel, *SeasonLabel, *LocateLabel, *VerseLabel;
QLineEdit *ThemeEdit, *SeasonEdit, *LocateEdit, *VerseEdit;
};
class MyPage3 : public QWidget
{
Q_OBJECT
public:
MyPage3(QWidget *parent = 0);
~MyPage3(){};
QLabel *ThemeLabel, *SeasonLabel, *LocateLabel, *VerseLabel;
QLineEdit *ThemeEdit, *SeasonEdit, *LocateEdit, *VerseEdit;
};
#endif // MYPAGE_H
config.cpp文件
#include "config.h"
config::config(QWidget *parent)
: QWidget(parent)
{
list = new QListWidget;
list->setIconSize(QSize(200, 100));//设置图片尺寸
list->setSpacing(12);
list->setViewMode(QListView::IconMode);//从左至右排列,上图下字,上图下字,上图下字
list->setMaximumWidth(134);//设置最大宽度
list->setMovement(QListView::Static);//设置图片不可拖动
QListWidgetItem *item1 = new QListWidgetItem;//定义项目
item1->setIcon(QIcon(":/xueren.png"));//设置按钮图标
item1->setText(tr("雪人"));//tr函数:里面的字符串需要国际化
QListWidgetItem *item2 = new QListWidgetItem;
item2->setIcon(QIcon(":/zise.jpg"));
item2->setText(tr("紫色"));
QListWidgetItem *item3 = new QListWidgetItem;
item3->setIcon(QIcon(":/fense.png"));
item3->setText(tr("粉色"));
list->addItem(item1);
list->addItem(item2);
list->addItem(item3);
QPushButton *btn = new QPushButton(this);
connect(btn, SIGNAL(clicked()), this, SLOT(close()));
btn->setText(tr("关闭"));
InfoWidget = new QStackedWidget;
page1 = new MyPage1;
page2 = new MyPage2;
page3 = new MyPage3;
InfoWidget->addWidget(page1);
InfoWidget->addWidget(page2);
InfoWidget->addWidget(page3);
connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(pagechanged(int)));
QHBoxLayout *hlayout = new QHBoxLayout;//横向布局
hlayout->addWidget(list);
hlayout->addWidget(InfoWidget);
QGridLayout *mainlayout = new QGridLayout(this);//纵向布局
mainlayout->addLayout(hlayout, 0, 0, 1, 6);//0行0列,占用1行6列
mainlayout->addWidget(btn, 1, 5, 1, 1);//1行5列,占用1行1列
/* QVBoxLayout *vlayout = new QVBoxLayout(this);//布局管理器
vlayout->addWidget(list);
vlayout->addWidget(btn);
this->setLayout(vlayout);*/
this->resize(800, 600);//设置窗口大小
}
config::~config()
{
}
void config::pagechanged(int index)
{
InfoWidget->setCurrentIndex(index);
}
MyPage.cpp文件
#include "MyPage.h"
MyPage1::MyPage1(QWidget *parent)
: QWidget(parent)
{
ThemeLabel = new QLabel(tr("主题:"));
SeasonLabel = new QLabel(tr("地点:"));
LocateLabel = new QLabel(tr("时间:"));
VerseLabel = new QLabel(tr("诗句:"));
ThemeEdit = new QLineEdit(tr("白雪纷飞"));
SeasonEdit = new QLineEdit(tr("冬天"));
LocateEdit = new QLineEdit(tr("北国"));
VerseEdit = new QLineEdit(tr("梅须逊雪三分白, 雪却输梅一段香"));
QGridLayout *Glayout = new QGridLayout(this);
Glayout->addWidget(ThemeLabel, 0, 0, 1, 2);//行宽为1,列宽为2
Glayout->addWidget(ThemeEdit, 0, 2, 1, 2);
Glayout->addWidget(SeasonLabel, 1, 0, 1, 2);
Glayout->addWidget(SeasonEdit, 1, 2, 1, 2);
Glayout->addWidget(LocateLabel, 2, 0, 1, 2);
Glayout->addWidget(LocateEdit, 2, 2, 1, 2);
this->setLayout(Glayout);
}
MyPage2::MyPage2(QWidget *parent)
: QWidget(parent)
{
ThemeLabel = new QLabel(tr("主题:"));
SeasonLabel = new QLabel(tr("地点:"));
LocateLabel = new QLabel(tr("时间:"));
VerseLabel = new QLabel(tr("诗句:"));
ThemeEdit = new QLineEdit(tr("紫气东来"));
SeasonEdit = new QLineEdit(tr("夏天"));
LocateEdit = new QLineEdit(tr("*"));
VerseEdit = new QLineEdit(tr(" 草木知春不久归,百般红紫斗芳菲"));
QGridLayout *Glayout = new QGridLayout(this);
Glayout->addWidget(ThemeLabel, 0, 0, 1, 2);//行宽为1,列宽为2
Glayout->addWidget(ThemeEdit, 0, 2, 1, 2);
Glayout->addWidget(SeasonLabel, 1, 0, 1, 2);
Glayout->addWidget(SeasonEdit, 1, 2, 1, 2);
Glayout->addWidget(LocateLabel, 2, 0, 1, 2);
Glayout->addWidget(LocateEdit, 2, 2, 1, 2);
this->setLayout(Glayout);
}
MyPage3::MyPage3(QWidget *parent)
: QWidget(parent)
{
ThemeLabel = new QLabel(tr("主题:"));
SeasonLabel = new QLabel(tr("地点:"));
LocateLabel = new QLabel(tr("时间:"));
VerseLabel = new QLabel(tr("诗句:"));
ThemeEdit = new QLineEdit(tr("红粉青楼"));
SeasonEdit = new QLineEdit(tr("春天"));
LocateEdit = new QLineEdit(tr("九州"));
VerseEdit = new QLineEdit(tr(" 青青河畔草,郁郁园中柳"));
QGridLayout *Glayout = new QGridLayout(this);
Glayout->addWidget(ThemeLabel, 0, 0, 1, 2);//行宽为1,列宽为2
Glayout->addWidget(ThemeEdit, 0, 2, 1, 2);
Glayout->addWidget(SeasonLabel, 1, 0, 1, 2);
Glayout->addWidget(SeasonEdit, 1, 2, 1, 2);
Glayout->addWidget(LocateLabel, 2, 0, 1, 2);
Glayout->addWidget(LocateEdit, 2, 2, 1, 2);
this->setLayout(Glayout);
}
main.cpp文件
#include <QtGui/QApplication>
#include "config.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
config w;
w.show();
return a.exec();
}
下一篇: QT5开发||09、QT5布局管理