Qt窗口随着控件显示(隐藏)而变大(变小)

此例子主要实现窗口随着窗口内控件的显示(隐藏)而变化大小

参考例子https://doc.qt.io/qt-5/qtwidgets-dialogs-extension-example.html

Qt窗口随着控件显示(隐藏)而变大(变小)_第1张图片

具体代码如下

CTestDlg::CTestDlg(QWidget *parent)
	: QDialog(parent)
{
	QGridLayout *gridLayout = new QGridLayout;

	gridLayout = new QGridLayout(this);
	gridLayout->setSpacing(6);
	gridLayout->setContentsMargins(11, 11, 11, 11);
	gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
	gridLayout->setSizeConstraint(QLayout::SetFixedSize); //主要是这个
	QPushButton *pushButton = new QPushButton("button",this);
	pushButton->setObjectName(QString::fromUtf8("pushButton"));
	connect(pushButton,SIGNAL(clicked()),this,SLOT(slot_pushButton_clicked()));

	gridLayout->addWidget(pushButton, 0, 0, 1, 1);

	m_tableView = new QTableView(this);
	m_tableView->setObjectName(QString::fromUtf8("tableView"));

	gridLayout->addWidget(m_tableView, 0, 1, 2, 1);

	QSpacerItem *verticalSpacer = new QSpacerItem(20, 276, QSizePolicy::Minimum, QSizePolicy::Expanding);

	gridLayout->addItem(verticalSpacer, 1, 0, 1, 1);



	setLayout(gridLayout);

}

CTestDlg::~CTestDlg()
{

}

void CTestDlg::slot_pushButton_clicked()
{
	m_tableView->setVisible(!m_tableView->isVisible());
}

 

你可能感兴趣的:(Qt)