各位版主,大侠,小弟在用QT做
界面编辑,要用到最简单的布局管理器,以下是试验的代码,但总出不来想要的界面。麻烦各位帮帮看看究竟错在哪里了。谢谢了。
#include <qapplication.h>
#include <qmainwindow.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
class MainWindow: public QMainWindow
{
public:
MainWindow()
{
QPushButton* Start = new QPushButton( this,"Start" );
QPushButton* Stop = new QPushButton( this,"Stop" );
QPushButton* Quit = new QPushButton( this,"Quit" );
QVBoxLayout* rightlayout = new QVBoxLayout;
rightlayout->addWidget( Start);
rightlayout->addWidget( Stop );
rightlayout->addWidget( Quit );
rightlayout->addStretch(1);
}
};
int main(int argc, char **argv)
{
QApplication a(argc, argv);
MainWindow mainWindow;
a.setMainWidget(&mainWindow);
mainWindow.resize(800,600);
mainWindow.show();
return a.exec();
}
下面是程序执行后
显示的图,但是并不是我希望的那样,三个
按钮竖直排列,且
没有显示我给各个按钮起的名字。为什么啊?!
以上
问题已经解决。谢谢三楼的提醒。
应该是把QVBoxLayout* rightlayout = new QVBoxLayout ;改为QVBoxLayout* rightlayout = new QVBoxLayout(this);就可以,我基本上是照着《
C++ GUI QT3 编程》这本书140页写的。 现在发现书上的也不一定对啊。
但是现在新问题又出现了,我把MainWindow()构造函数的内容换为:
QLabel *label = new
QLabel("Timer Interval",this);
QwtCounter *counter = new QwtCounter(this);
counter->setRange(0.0,100.0,1.0);
QHBoxLayout *layoutfirst = new QHBoxLayout(this);
layoutfirst->addWidget( label );
layoutfirst->addWidget( counter );
QVBoxLayout *leftlayout = new QVBoxLayout(this);
leftlayout->addLayout( layoutfirst );
QPushButton* Start = new QPushButton( this,"Start" );
Start->setText( tr( "&Start" ) );
QPushButton* Stop = new QPushButton( this,"Stop" );
Stop->setText( tr( "&Stop" ) );
QPushButton* Quit = new QPushButton( this,"Quit" );
Quit->setText( tr( "&Quit" ) );
QVBoxLayout* rightlayout = new QVBoxLayout(this);
rightlayout->addWidget( Start);
rightlayout->addWidget( Stop );
rightlayout->addWidget( Quit );
QHBoxLayout* mainlayout = new QHBoxLayout(this);
mainlayout->setMargin(11);
mainlayout->setSpacing(6);
mainlayout->addLayout(rightlayout);
mainlayout->addLayout(leftlayout);
我主要想实现一下多重的这种布局嵌套,但是好像出不来我想要的布局,很纠结,不知道为啥,为毕设,在这块已经耗掉了我快整整两周了。大侠帮忙解答下为啥啊。最终的显示图形见下面第二个图。
[ 此帖被jiayouwyhit在2011-03-27 15:01重新编辑 ]