1.Qt删除文件夹操作
非空文件夹不能直接删除,需要递归删除里面的文件;
使用封装函数removeRecursively()直接删除
bool DeleteFileOrFolder(const QString &strPath)//要删除的文件夹或文件的路径
{
if (strPath.isEmpty() || !QDir().exists(strPath))//是否传入了空的路径||路径是否存在
return false;
QFileInfo FileInfo(strPath);
if (FileInfo.isFile())//如果是文件
QFile::remove(strPath);
else if (FileInfo.isDir())//如果是文件夹
{
QDir qDir(strPath);
qDir.removeRecursively();
}
return true;
}
2.边框四周阴影的添加
setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground, true); //窗口半透明显示,不然阴影为黑色
//设置具体阴影
QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
shadow_effect->setOffset(0, 0);
//阴影颜色
shadow_effect->setColor(QColor(38, 78, 119, 127));
//阴影半径
shadow_effect->setBlurRadius(8);
setGraphicsEffect(shadow_effect);
踩坑:
选择绘制阴影的控件时注意设置阴影效果的是哪个窗口,底部widget和上一层widget设置margin以显示阴影。此报错不影响编译运行,会造成窗口卡顿和error提示
3.C++字符串比较
使用"=="运算符比较时出现警告并且判断无法进入
仅在内部为原始类型(如int、char、bool…)实现,而 const char* 不是原始类型
int strcmp ( const char * str1, const char * str2 );
//C库函数
int compare (const string& string-name) const;
//相等小于0,1小于2返回小于0,大于则返回大于0;
//C++内置函数
4.tableWidget和tableView
tableWidget继承自tableView。tableView单行超出列宽时自动换行,使用setWordWrap(false)将换行效果改为单行显示,超出部分用省略号显示,同时增加光标指向时的tootip效果
ui->tableView->setMouseTracking(true);//增加鼠标追踪
connect(ui->tableWidget,SIGNAL(entered(QModelIndex)),this,SLOT(ShowTooltip(QModelIndex)));
void MainWindow::ShowTooltip(QModelIndex index)
{
QToolTip::showText(QCursor::pos(),index.data().toString());
return ;
}
ui.tableView->setFocusPolicy(Qt::NoFocus); //去掉选中时的虚线框
5.添加程序图标报错Debug error1
ico文件需要特殊的格式转换,不能通过修改后缀将其他格式改为ico格式
6.setContentsMargins 部件距离四边的距离
setContentsMargins(int left, int top, int right, int bottom );
7.text-align
只支持QPushButton and QProgressBar
可使用setAlignment(Qt::AlignHCenter) 修改相对位置
8.addWidget 添加子控件到layout,可以设置缩放因子
addWidget(QWidget *, int stretch = 0, Qt::Alignment alignment = 0);
addSpacing 添加指定大小的空白空间
addStretch 添加指定缩放因子的可缩放空白空间,最小宽度为0
setContentsMargins 设置外边距
setSpacing 设置子控件间距
9.查找是否包含某字符串
Qt::CaseSensitive:大小写敏感
QString str.contains("yes",Qt::CaseSensitive);