1.布局中加弹簧
pVLayout->addSpacerItem(new QSpacerItem(0, 100, QSizePolicy::Minimum, QSizePolicy::Expanding));
2.QWidget设置圆角显示
QWidget *pMainWgt = new QWidget();
pMainWgt->setStyleSheet("background-color:rgb(100,100,100);"
"border:1px solid #505055;"
"border-radius:10px; ");
pMainWgt->setLayout(pVLayout);
3.ui文件复用
//将.ui文件复制到程序目录下,添加现有文件到编辑器,执行qmake
//头文件添加
#include "ui_TestWgt.h"
{
private:
Ui::TestWgt *m_Ui;
}
//实现中添加
{
QWidget *pWgt = new QWidget();
m_Ui= new Ui::TestWgt();
m_Ui->setupUi(pWgt);
QHBoxLayout *plyt = new QHBoxLayout;
plyt->addWidget(pWgt);
this->setLayout(plyt);
}
4.判断鼠标在某一控件区域内
if(m_btn->geometry().contains(this->mapFromGlobal(QCursor::pos())))
5.清除布局内控件
//清空horizontalLayout布局内的所有元素
QLayoutItem *child;
while ((child = horizontalLayout->takeAt(0)) != 0)
{
//setParent为NULL,防止删除之后界面不消失
if(child->widget())
{
child->widget()->setParent(NULL);
}
delete child;
}
6.Release版本debug使断点调试生效
CONFIG += rel_with_dbg_info
7.QString转const char*
QString str = "hello world";
const char *cststr = qPrintable(str);