代码很简单
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private:
QPushButton *button;
};
#endif // WIDGET_H
main.cpp
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(500,500);
a.setActiveWindow(&w);
w.show();
return a.exec();
}
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
setMinimumSize(500,250);
setMaximumSize(500,300);
resize(500,600);
button = new QPushButton("Quit");
button->resize(20,30);
//this->button->show();
connect(button,SIGNAL(clicked()),this,SLOT(close()));
}
Widget::~Widget()
{
delete button;
}
这个widget界面怎么不显示button,加一个button->show(),button会在另一个界面显示出来,刚学,不理解,求解释
------解决方案--------------------
#include "widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
setMinimumSize(500,250);
setMaximumSize(500,300);
resize(500,600);
button = new QPushButton(this);这里改改
button->resize(20,30);
//this->button->show();
connect(button,SIGNAL(clicked()),this,SLOT(close()));
}
Widget::~Widget()
{
delete button;
}
------解决方案--------------------
你的button和w没关系啊,要建立关系,可以在new这个button时指定w为其父对象