但实际上是这样的效果
窗体本身很窄,怎么调整窗体的尺寸?
代码如下:
#include "stackdlg.h"
#include <QHBoxLayout>
StackDlg::StackDlg(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle(tr("StackedWidget"));
list =new QListWidget(this);
list->insertItem(0,tr("Window1"));
list->insertItem(1,tr("Window2"));
list->insertItem(2,tr("Window3"));
label1 =new QLabel(tr("WindowTest1"));
label2 =new QLabel(tr("WindowTest2"));
label3 =new QLabel(tr("WindowTest3"));
stack =new QStackedWidget(this);
stack->addWidget(label1);
stack->addWidget(label2);
stack->addWidget(label3);
QHBoxLayout *mainLayout =new QHBoxLayout(this);
mainLayout->addWidget(list);
mainLayout->addWidget(stack, 0, Qt::AlignRight);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->setStretchFactor(list,1);
mainLayout->setStretchFactor(stack,3);
connect(list,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));
}
StackDlg::~StackDlg()
{
}
QT新手,虚心求教~~
10 个解决方案
#1
直接粘到 你的 ui->setupUi(this); 下面
setWindowTitle(tr("StackedWidget"));
QListWidget* list = new QListWidget(this);
list->insertItem(0, tr("Window1"));
list->insertItem(1, tr("Window2"));
list->insertItem(2, tr("Window3"));
QLabel* label1 = new QLabel(tr("WindowTest1"));
QLabel* label2 = new QLabel(tr("WindowTest2"));
QLabel* label3 = new QLabel(tr("WindowTest3"));
QStackedWidget* stack = new QStackedWidget(this);
stack->addWidget(label1);
stack->addWidget(label2);
stack->addWidget(label3);
QHBoxLayout* mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(list);
mainLayout->addWidget(stack, 0, Qt::AlignCenter | Qt::AlignHCenter);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->setStretchFactor(list, 1);
mainLayout->setStretchFactor(stack, 3);
connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
#2
然而我创建的工程并没有.ui文件啊,主函数如下图:
#3
加这两句就好了
list->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
stack->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
list->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
stack->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
#4
还是不行啊,亲。
#5
自己顶一下,不要沉了~继续求大神指导
#6
不能沉啊,问题依然没有解决~依然求大神指导
#7
在你设置布局的前后加三句话
QWidget *widget = new QWidget();
this->setCentralWidget(widget);
QHBoxLayout *mainLayout =new QHBoxLayout;
mainLayout->addWidget(list);
mainLayout->addWidget(stack,0,Qt::AlignRight);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->setStretchFactor(list,1);
mainLayout->setStretchFactor(stack,3);
centralWidget()->setLayout(mainLayout);
因为你继承的是QMainWindow,本身已经有布局了,所以会有问题。或者你可以重新建个项目,继承QDialog就不会有这个问题了。
#8
我这里没啥问题,可能是因为你的不是在windows系统下,自己多试试,例如:固定list的宽高
setWindowTitle(tr("StackedWidget"));
QListWidget *list = new QListWidget(this);
list->insertItem(0,tr("Window1"));
list->insertItem(1,tr("Window2"));
list->insertItem(2,tr("Window3"));
QLabel *label1 = new QLabel(tr("WindowTest1"));
label1->setAlignment(Qt::AlignCenter);
QLabel *label2 = new QLabel(tr("WindowTest2"));
label2->setAlignment(Qt::AlignCenter);
QLabel *label3 = new QLabel(tr("WindowTest3"));
label3->setAlignment(Qt::AlignCenter);
QStackedWidget *stack = new QStackedWidget(this);
stack->addWidget(label1);
stack->addWidget(label2);
stack->addWidget(label3);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(list, 1);
mainLayout->addWidget(stack, 3);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
setWindowTitle(tr("StackedWidget"));
QListWidget *list = new QListWidget(this);
list->insertItem(0,tr("Window1"));
list->insertItem(1,tr("Window2"));
list->insertItem(2,tr("Window3"));
QLabel *label1 = new QLabel(tr("WindowTest1"));
label1->setAlignment(Qt::AlignCenter);
QLabel *label2 = new QLabel(tr("WindowTest2"));
label2->setAlignment(Qt::AlignCenter);
QLabel *label3 = new QLabel(tr("WindowTest3"));
label3->setAlignment(Qt::AlignCenter);
QStackedWidget *stack = new QStackedWidget(this);
stack->addWidget(label1);
stack->addWidget(label2);
stack->addWidget(label3);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(list, 1);
mainLayout->addWidget(stack, 3);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
#9
在你设置布局的前后加三句话
QWidget *widget = new QWidget();
this->setCentralWidget(widget);
QHBoxLayout *mainLayout =new QHBoxLayout;
mainLayout->addWidget(list);
mainLayout->addWidget(stack,0,Qt::AlignRight);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->setStretchFactor(list,1);
mainLayout->setStretchFactor(stack,3);
centralWidget()->setLayout(mainLayout);
因为你继承的是QMainWindow,本身已经有布局了,所以会有问题。或者你可以重新建个项目,继承QDialog就不会有这个问题了。
按照你说的方法,真的成功了!多谢!
QWidget *widget = new QWidget();
this->setCentralWidget(widget);
QHBoxLayout *mainLayout =new QHBoxLayout;
mainLayout->addWidget(list);
mainLayout->addWidget(stack,0,Qt::AlignRight);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->setStretchFactor(list,1);
mainLayout->setStretchFactor(stack,3);
centralWidget()->setLayout(mainLayout);
因为你继承的是QMainWindow,本身已经有布局了,所以会有问题。或者你可以重新建个项目,继承QDialog就不会有这个问题了。
按照你说的方法,真的成功了!多谢!
#10
正确答案在上一楼,是我在创建窗口的时候大意了 不过还是多谢
#1
直接粘到 你的 ui->setupUi(this); 下面
setWindowTitle(tr("StackedWidget"));
QListWidget* list = new QListWidget(this);
list->insertItem(0, tr("Window1"));
list->insertItem(1, tr("Window2"));
list->insertItem(2, tr("Window3"));
QLabel* label1 = new QLabel(tr("WindowTest1"));
QLabel* label2 = new QLabel(tr("WindowTest2"));
QLabel* label3 = new QLabel(tr("WindowTest3"));
QStackedWidget* stack = new QStackedWidget(this);
stack->addWidget(label1);
stack->addWidget(label2);
stack->addWidget(label3);
QHBoxLayout* mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(list);
mainLayout->addWidget(stack, 0, Qt::AlignCenter | Qt::AlignHCenter);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->setStretchFactor(list, 1);
mainLayout->setStretchFactor(stack, 3);
connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
#2
然而我创建的工程并没有.ui文件啊,主函数如下图:
#3
加这两句就好了
list->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
stack->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
list->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
stack->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
#4
还是不行啊,亲。
#5
自己顶一下,不要沉了~继续求大神指导
#6
不能沉啊,问题依然没有解决~依然求大神指导
#7
在你设置布局的前后加三句话
QWidget *widget = new QWidget();
this->setCentralWidget(widget);
QHBoxLayout *mainLayout =new QHBoxLayout;
mainLayout->addWidget(list);
mainLayout->addWidget(stack,0,Qt::AlignRight);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->setStretchFactor(list,1);
mainLayout->setStretchFactor(stack,3);
centralWidget()->setLayout(mainLayout);
因为你继承的是QMainWindow,本身已经有布局了,所以会有问题。或者你可以重新建个项目,继承QDialog就不会有这个问题了。
#8
我这里没啥问题,可能是因为你的不是在windows系统下,自己多试试,例如:固定list的宽高
setWindowTitle(tr("StackedWidget"));
QListWidget *list = new QListWidget(this);
list->insertItem(0,tr("Window1"));
list->insertItem(1,tr("Window2"));
list->insertItem(2,tr("Window3"));
QLabel *label1 = new QLabel(tr("WindowTest1"));
label1->setAlignment(Qt::AlignCenter);
QLabel *label2 = new QLabel(tr("WindowTest2"));
label2->setAlignment(Qt::AlignCenter);
QLabel *label3 = new QLabel(tr("WindowTest3"));
label3->setAlignment(Qt::AlignCenter);
QStackedWidget *stack = new QStackedWidget(this);
stack->addWidget(label1);
stack->addWidget(label2);
stack->addWidget(label3);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(list, 1);
mainLayout->addWidget(stack, 3);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
setWindowTitle(tr("StackedWidget"));
QListWidget *list = new QListWidget(this);
list->insertItem(0,tr("Window1"));
list->insertItem(1,tr("Window2"));
list->insertItem(2,tr("Window3"));
QLabel *label1 = new QLabel(tr("WindowTest1"));
label1->setAlignment(Qt::AlignCenter);
QLabel *label2 = new QLabel(tr("WindowTest2"));
label2->setAlignment(Qt::AlignCenter);
QLabel *label3 = new QLabel(tr("WindowTest3"));
label3->setAlignment(Qt::AlignCenter);
QStackedWidget *stack = new QStackedWidget(this);
stack->addWidget(label1);
stack->addWidget(label2);
stack->addWidget(label3);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(list, 1);
mainLayout->addWidget(stack, 3);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
#9
在你设置布局的前后加三句话
QWidget *widget = new QWidget();
this->setCentralWidget(widget);
QHBoxLayout *mainLayout =new QHBoxLayout;
mainLayout->addWidget(list);
mainLayout->addWidget(stack,0,Qt::AlignRight);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->setStretchFactor(list,1);
mainLayout->setStretchFactor(stack,3);
centralWidget()->setLayout(mainLayout);
因为你继承的是QMainWindow,本身已经有布局了,所以会有问题。或者你可以重新建个项目,继承QDialog就不会有这个问题了。
按照你说的方法,真的成功了!多谢!
QWidget *widget = new QWidget();
this->setCentralWidget(widget);
QHBoxLayout *mainLayout =new QHBoxLayout;
mainLayout->addWidget(list);
mainLayout->addWidget(stack,0,Qt::AlignRight);
mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->setStretchFactor(list,1);
mainLayout->setStretchFactor(stack,3);
centralWidget()->setLayout(mainLayout);
因为你继承的是QMainWindow,本身已经有布局了,所以会有问题。或者你可以重新建个项目,继承QDialog就不会有这个问题了。
按照你说的方法,真的成功了!多谢!
#10
正确答案在上一楼,是我在创建窗口的时候大意了 不过还是多谢