如何使用Qt setWindowFilePath
我似乎无法让 setWindowFilePath 在我的任何项目中工作。该值已存储并且可以检索,但它从未显示在我的应用程序的标题栏中。它在我下载的示例应用程序中确实可以正常工作,但我找不到它们的不同之处。不管怎样,这是我创建的一个简单的应用程序来演示这个问题。我粘贴了下面 3 个文件 mainwin.h、main.cpp 和 mainwin.cpp 中的代码。
有什么想法吗?我在 Windows 7 上使用 Qt 4.6.3 和 MS 编译器。
#ifndef MAINWIN_H
#define MAINWIN_H
#include <QMainWindow>
class mainwin : public QMainWindow
{
Q_OBJECT
public:
explicit mainwin(QWidget *parent = 0);
signals:
public slots:
};
#endif // MAINWIN_H
#include "mainwin.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setApplicationName("my test");
app.setOrganizationName("NTFMO");
mainwin window;
window.show();
return app.exec();
}
#include "mainwin.h"
mainwin::mainwin(QWidget *parent) :
QMainWindow(parent)
{
setWindowFilePath("C:\asdf.txt");
}
I can't seem to get setWindowFilePath to work in any of my projects. The value is stored and can be retrieved, but it never shows up in the title bar of my app. It does work correctly in a sample app I downloaded, but I can't find what they do differently. Anyway, here's a simple app I created to demonstrate the problem. I pasted the code from the 3 files, mainwin.h, main.cpp, and mainwin.cpp below.
Any ideas? I'm using Qt 4.6.3 on Windows 7, with the MS compiler.
#ifndef MAINWIN_H
#define MAINWIN_H
#include <QMainWindow>
class mainwin : public QMainWindow
{
Q_OBJECT
public:
explicit mainwin(QWidget *parent = 0);
signals:
public slots:
};
#endif // MAINWIN_H
#include "mainwin.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setApplicationName("my test");
app.setOrganizationName("NTFMO");
mainwin window;
window.show();
return app.exec();
}
#include "mainwin.h"
mainwin::mainwin(QWidget *parent) :
QMainWindow(parent)
{
setWindowFilePath("C:\asdf.txt");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是QTBUG-16507。
简单的解决方法(刚刚在我的项目中测试过)是:
它只会将标题设置为您在小部件显示之前使用的值(在构造函数中,在您的情况下)。就像魅力一样。
It is QTBUG-16507.
And easy workaround (just tested it in my project) is:
It will just set title to value that you used before widget show (in constructor, in your case). Works like a charm.
由于某种原因,从 QMainWindow 的构造函数调用时,
setWindowFilePath()
似乎不起作用。但您可以使用单次计时器:并记住在文字路径中使用 \\ 而不是 \
For some reason,
setWindowFilePath()
does not seem to work when called from QMainWindow's constructor. But you can use single shot timer:And remember to use \\ in literal paths instead of \
我刚刚发现使用QTimer::singleShot,显然没有办法传递参数。要传递参数(在我的例子中,是使用 QSettings 检索的文件路径),请使用:
I just discovered that with QTimer::singleShot, apparently there is no way to pass parameters. To pass parameters (in my case, the file path retrieved using QSettings), use: