QT 小知识点
0 QT 项目转VS项目CMD 转到工程目录然后输入qmake –tp vc1.格式化数据QString str = QString("%1%2").arg(QTStr("屏幕"),QString::number(i+1));QString str = QString("%1 %2: %3x%4 @ %5,%6")....
0 QT 项目转VS项目
CMD 转到工程目录 然后输入qmake –tp vc
![]()

1. 格式化数据
QString str = QString("%1%2").
arg(QTStr("屏幕"),
QString::number(i+1));
QString str = QString("%1 %2: %3x%4 @ %5,%6").
arg(QTStr("Display"),
QString::number(i),
QString::number((int)screenGeometry.width()),
QString::number((int)screenGeometry.height()),
QString::number((int)screenGeometry.x()),
QString::number((int)screenGeometry.y()));
2 中文乱码
#define QTStr(lookupVal) QString::fromUtf8(Str(lookupVal))
方法一
QString::fromLocal8Bit("你好中国")
1
方法二
QStringLiteral("你好中国")
1
方法三
在.cpp中加入
#pragma execution_character_set("utf-8")
参考资料:https://blog.csdn.net/qq_36323886/article/details/76595794
3 QString 转 std::string
QString的成员函数toStdString()
转char*
QString newSourceName = QString("%1%2").arg(QTStr("摄像头"), QString::number(nCam + 1));
obs_source_set_name(sourceSelect.newSource, newSourceName.toStdString().c_str());
4 判断文件是否存在
包含头文件:<QFileInfo>
代码:
QFileInfo file("文件路径");
if(file.exists()==false)
{文件不存在;}
5 程序所在路径
QString QCoreApplication::applicationDirPath()
参考 https://blog.csdn.net/liyuanbhu/article/details/53710249
6 QT定时器延时处理
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(AddBkEndSource()));
timer->setSingleShot(true); //只发送一次信号
timer->start(1000);
7 QMessageBox使用
void LoginDialog::accept(){
if (QMessageBox::question(this,
tr("Quit"),
tr("Are you sure to quit this application?"),
QMessageBox::Yes, QMessageBox::No)
== QMessageBox::Yes){
return QDialog::accept(); //达到某种条件后,再关闭
}
else
;
}
8 设置窗口初始大小
1)QCreator工具里设置
2)resize()设置
3)setFixedSize setMaximumSize 使用这种方法 不能调整窗口大小
在Qt中控制窗口大小控制窗口大小常用的函数:
void setMinimumSize ( const QSize &amp; )
virtual void setMinimumSize ( int minw, int minh )
void setMaximumSize ( const QSize &amp; )
virtual void setMaximumSize ( int maxw, int maxh )
void setMinimumWidth ( int minw )
void setMinimumHeight ( int minh )
void setMaximumWidth ( int maxw )
void setMaximumHeight ( int maxh )
参考https://blog.csdn.net/fanyunda1988/article/details/52067836
9 去掉标题栏
this->setWindowFlags(Qt::FramelessWindowHint);
去掉后,重载mousePressEvent、mouseMoveEvent、mouseReleaseEvent 移动窗口
//可以在构造函数中初始一下last变量用其成员函数setX,setY就是了
//接下来就是对三个鼠标事件的重写
void MainWindow::mousePressEvent(QMouseEvent *e)
{
last = e->globalPos();
}
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
int dx = e->globalX() - last.x();
int dy = e->globalY() - last.y();
last = e->globalPos();
move(x()+dx, y()+dy);
}
void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
int dx = e->globalX() - last.x();
int dy = e->globalY() - last.y();
move(x()+dx, y()+dy);
}
参考https://blog.csdn.net/u010352603/article/details/51368075
10 在任务栏中 设置应用图标
setWindowTitle(QStringLiteral("登录"));
setWindowIcon(QIcon(":/my/images/my/xnw.ico"));
11 打开本地视频文件
假设,要用默认播放器打开 "E:\\视频文件\\2_2021-02-07_14-20-16.flv
可以直接这么写:
QString run_path = "cmd /c " + filePath_;
QProcess *p = new QProcess(this);
p->start(run_path);
但是如果要打开类似这样的带空格的文件 E:\\视频 文件\\2_2021-02-07_14-20-16.flv
这样则会失败,因为CMD根据空格划分参数
可以再路径上,加上"",也就是: "E:\\视频 文件\\2_2021-02-07_14-20-16.flv"
//路径中有空格 整个路径加""
QString dealFilePath = QString("\"%1\"").arg(filePath_);
QString run_path = "cmd /c " + dealFilePath;
QProcess *p = new QProcess(this);
p->start(run_path);
更多推荐



所有评论(0)