示例代码:
1、头文件
class MainWindow : public QMainWindow
{
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
2、实现文件
代码1
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QLabel label=new QLabel(this);
label.setGeometry(10,10,100,20);
QDateTime *datetime=new QDateTime(QDateTime::currentDateTime());
label.setText(datetime->date().toString());
}
这个是可以正常显示的,在mainwindow中有个label标签显示时间。
但是我将上述实现代码改为:
代码2
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QLabel label;
label.setParent(this);
label->setGeometry(10,10,100,20);
QDateTime *datetime=new QDateTime(QDateTime::currentDateTime());
label->setText(datetime->date().toString());
}
之后就不能显示label标签,这是为什么?
我在mainwindow的声明中添加一个label成员,对这个成员使用代码2同样可以显示,这又是为什么?