前言
本页汇总了QT开发中遇到的相关问题,记录方便后期查阅。
Qt5 教程
一个非常不错的QT教程
https://www.bogotobogo.com/Qt/Qt5_TutorialHelloWorld.php
问题汇总
打开文件获取路径
获取文件路径:
static QString getOpenFileName(QWidget *parent = Q_NULLPTR,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = Q_NULLPTR,
Options options = Options());
参数1:父窗口
参数2:弹出界面的菜单栏文字
参数3:打开的初始目录 比如“/”表示根目录
参数4:设置打开的文件类型
参数5:看代码。。。
示例代码:
void Widget::slotBtnClick()
{
QString s = QFileDialog::getOpenFileName(
this, "选择要播放的文件",
"/",
"视频文件 (*.flv *.rmvb *.avi *.mp4);; 所有文件 (*.*);; ");
qDebug() << "path=" << s;
if (!s.isEmpty())
{
}
}
示例代码:
QString MainWindow::getFileFullUrl()
{
QString strFileFullUrl = "";
QFileDialog fileDialog(this);
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setWindowTitle(tr("Open Files"));
fileDialog.setFileMode(QFileDialog::ExistingFiles);
if (fileDialog.exec() == QDialog::Accepted) {
QList<QUrl> urls = fileDialog.selectedUrls();
for (auto &url: urls) {
QString filename = url.fileName();
QString strUrl = url.toLocalFile();
if(!filename.isEmpty() && !strUrl.isEmpty()) {
strFileFullUrl = strUrl;
} else {
QString strtitle = url.toString();
QString strInfo = tr("Empty: filename:[") + filename + tr("]");
QMessageBox::information(nullptr, strtitle, strInfo,
QMessageBox::Abort, QMessageBox::Abort);
}
}
}
return strFileFullUrl;
}
选择保存文件的文件夹
示例代码:
QString file_path = QFileDialog::getExistingDirectory(this,"请选择模板保存路径...","./");
if(file_path.isEmpty())
{
return;
}else{
qDebug() << file_path << endl;
}
QString getExistingDirectory(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), Options options = ShowDirsOnly)
第三参数dir,这个参数可以指定一个路径(绝对/相对皆可),在对话框打开的时候默认先到这个路径中进行继续选择
相对路径示例
QString filepath = "E:/QtProjects/Qt4_Projects/download/QDir/Test1B/Resources/567.jpg"; //图片的绝对路径
QString curPath = QDir::currentPath(); //得到当前程序的工作路径为“E:/QtProjects/Qt4_Projects/download/QDir/Test1B”
QString relPath = filepath.mid(curPath.length()+1); //得到图片的相对路径“Resources/567.jpg”
获取桌面路径,exe路径
/**获取系统桌面路径
* @time: 2019-7-5
* @author: qiangu
* @return: desktopPath
*/
QString FileDirectory::getDesktopPath(){
QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
qDebug()<< "desktopPath:" << desktopPath;
return desktopPath;
}
/**获取的执行文件exe所在的路径
* @time: 2019-7-5
* @author: qiangu
* @return: applicationDirPath
*/
QString FileDirectory::getApplicationDirPath(){
// QString QApplication::applicationDirPath()
QString applicationDirPath = QApplication::applicationDirPath();
qDebug()<< "applicationDirPath:" << applicationDirPath;
return applicationDirPath;
}
//相对路径,常用于资源文件。在QtCreater中的“./”这个路径是bulid directory的路径
指定QOpenGLWidget和QOpenGLContext支持的OpenGL版本
// main.cpp
QSurfaceFormat glFormat;
glFormat.setVersion(3, 3);
glFormat.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(glFormat);
参考资料
https://blog.csdn.net/chentianveiko/article/details/52453403
https://blog.csdn.net/qq_37988830/article/details/87899332