当前位置: 代码迷 >> QT开发 >> 初学QT,不知道下面的程序窗口为什么显示不了pushbutton,求过路人指点,该如何处理
  详细解决方案

初学QT,不知道下面的程序窗口为什么显示不了pushbutton,求过路人指点,该如何处理

热度:107   发布时间:2016-04-25 04:45:24.0
初学QT,不知道下面的程序窗口为什么显示不了pushbutton,求过路人指点
[size=18px]MessageBoxs.cpp:
C/C++ code
#include "MessageBoxs.h"#include <QtGui>MessageBoxs::MessageBoxs(QWidget *parent):QDialog(parent){        setWindowTitle(tr("Custom test~~"));        QPushButton *PushButton_Custom = new QPushButton("Custom");        PushButton_Custom->setText(tr("Custom.."));        QGridLayout *GridLayout = new QGridLayout;        GridLayout->addWidget(PushButton_Custom,0,1);        GridLayout->setMargin(10);        GridLayout->setSpacing(20);        connect(PushButton_Custom,SIGNAL(clicked()),this,SLOT(Slot_Custom()));}MessageBoxs::~MessageBoxs(){}void MessageBoxs::Slot_Custom(){}


MessageBoxs.h:
C/C++ code
#ifndef MESSAGEBOX_H#define MESSAGEBOX_H#include <QDialog>class MessageBoxs:public QDialog{        Q_OBJECTpublic:        MessageBoxs(QWidget *parent = 0);        ~MessageBoxs();public slots:        void Slot_Custom();};#endif


main.cpp
C/C++ code
#include <qapplication.h>#include <QPushButton>#include "MessageBoxs.h"int main(int argc,char **argv){        QApplication app(argc,argv);        //QPushButton PushButton_Custom("hello world!!");        //PushButton_Custom.show();        MessageBoxs *MessageBoxs_Custom = new MessageBoxs();        MessageBoxs_Custom->show();        return app.exec();}
[/size]

------解决方案--------------------
因为你的MessageBox.cpp中虽然定义了GridLayout,但却没有将它加入到dialog中,修改如下:
MessageBoxs::MessageBoxs(QWidget *parent):QDialog(parent)
{
setWindowTitle(tr("Custom test~~"));

QPushButton *PushButton_Custom = new QPushButton("Custom");
PushButton_Custom->setText(tr("Custom.."));

QGridLayout *GridLayout = new QGridLayout;
GridLayout->addWidget(PushButton_Custom,0,1);
GridLayout->setMargin(10);
GridLayout->setSpacing(20);

this->setLayout(GridLayout);

connect(PushButton_Custom,SIGNAL(clicked()),this,SLOT(Slot_Custom()));
}
属于粗心大意犯的错误...
------解决方案--------------------
1#正解!

至于你说的,在Slot_Custom当中来设置不可行的原因
我觉得
第一:QT当中的界面布局应该是先于某些成员的!
第二:Slot_Custom这个函数只能设定更小的界面布局(就是总体布局的子布局!)。