【Qt】Qt的setVisible失效问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Strong_HCyouth/article/details/88176269
QWidget* DispatchIssueFrom::initTimeOut()
{

	timeWidget = new QWidget(this);
	QVBoxLayout *vLayout = new QVBoxLayout(timeWidget);
	time = new QLabel;
	time->setText(u8"15:00:00");
	time->setStyleSheet("QLabel{color:#3EC4FF; font:normal bold 22px 'Microsoft YaHei'}");
	QLabel *status = new QLabel;
	status->setText(u8"发布中");
	status->setStyleSheet("QLabel{color:#000000; font:normal normal 12px 'Microsoft YaHei'}");
	QLabel *spilliter = new QLabel;
	spilliter->setStyleSheet("QLabel{background-color:#F2F2F2;}");
	spilliter->setFixedSize(270, 1);

	vLayout->addWidget(time, 0, Qt::AlignHCenter);
	vLayout->addWidget(status, 0, Qt::AlignHCenter);
	vLayout->addWidget(spilliter, 0, Qt::AlignHCenter);

	timeWidget->setLayout(vLayout);
    return timeWidget;
}

{
    ……
    initTimeOut().setVisible(false);
    ……
}

【解决办法】:虽然封装了QWidget,但是每次调用的时候都会new一个新的QWidget,所以后面想要隐藏的Qwidget是最开始new的Widget。

【拓展】:

Calling setVisible(true) or show() sets the widget to visible status if all its parent widgets up to the window are visible. If an ancestor is not visible, the widget won't become visible until all its ancestors are shown. 

(调用setVisible(true)或show()将小部件设置为可见状态(前提是父窗口都是可见的)。如果某个父窗口不可见,那么在显示其所有父窗口之前,子窗口都是不可见的。)

virtual void setVisible(bool visible);
inline void setHidden(bool hidden) { setVisible(!hidden); }
inline void show() { setVisible(true); }
inline void hide() { setVisible(false); }

可以知道,其他方法都是依赖于setVisible()的。setVisible可以设置QWidget是否可见,但是不一定Widget被析构

猜你喜欢

转载自blog.csdn.net/Strong_HCyouth/article/details/88176269
QT
今日推荐