1. QLineEdit
添加控件后,可以编辑控件的名称,然后使用名称获取和设置值
QString qstrValue = ui->strName->text();
QMessageBox::information(this, "提示", qstrValue);
2.窗体导航切换
添加新的对话框,然后引入头文件创建变量,和MFC中一样
//跳转到子窗口
this->hide();
m_dlgSet.setModal(true);
m_dlgSet->setAttribute(Qt::WA_DeleteOnClose); //调用close后自动析构对象
m_dlgSet.show();
//子窗口返回父窗口
QDialog *pMainDlg = (QDialog *)parentWidget();
this->close();
pMainDlg->show();
布局
在窗体上添加控件,控件是不会随着窗体的大小变化而自动调整大小的。如果需要自动调整大小,添加布局 水平,垂直,栅格(矩阵/表格布局)
QMainWindow有一个中心区域,centralwidget
QT中没有加布局的容器控件在属性栏中会显示一个"不可用"图标,如:
加了布局之后图标会消失,如加了水平布局,则可以看到布局的一些属性
QPushButton *firstButton = new QPushButton();
QPushButton *secondButton = new QPushButton();
QPushButton *lastButton = new QPushButton();
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(firstButton);
layout->addWidget(secondButton);
layout->addWidget(lastButton);
//但是当应用在主窗口时,这样使用却没有效果…这是因为,主窗口默认是包含菜单栏,工具栏,中心窗口,状态栏等,
//而我们设置的布局不应该应用在整个主窗口中,而是应该对中心窗口使用
//this->setLayout(layout);
//创建一个窗口.设置窗口的布局.
QWidget *widget = new QWidget(this);
widget->setLayout(layout);
//然后对主窗口设置新的中心窗口.也就是你布局好的窗口.
this->setCentralWidget(widget);
设置widget边框,stylesheet设置 border:1px solid red;
background-color:rgb(255, 255, 255); //背景
背景图片
Qlabel使用 image: url(:/res/short_msg.png);
显示背景图片,图片大小能够自适应控件大小显示完整,res是资源根目录下的文件夹(添加资源的时候,图片是放在res目录下的)
qhboxlayout中添加widget,frame等控件时无法同一级别排列
解决方法:先添加widget,frame控件,然后选择要添加到qhboxlayout中的控件,右键点击布局