如何使用Qt setWindowFilePath

发布于 2024-09-14 17:01:38 字数 870 浏览 2 评论 0原文

我似乎无法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

财迷小姐 2024-09-21 17:01:38

它是QTBUG-16507

简单的解决方法(刚刚在我的项目中测试过)是:

/********************** HACK: QTBUG-16507 workaround **************************/
void MyMainWindow::showEvent(QShowEvent *event)
{
    QMainWindow::showEvent(event);
    QString file_path = windowFilePath();
    setWindowFilePath(file_path+"wtf we have some random text here");
    setWindowFilePath(file_path);
}
/******************************************************************************/

它只会将标题设置为您在小部件显示之前使用的值(在构造函数中,在您的情况下)。就像魅力一样。

It is QTBUG-16507.

And easy workaround (just tested it in my project) is:

/********************** HACK: QTBUG-16507 workaround **************************/
void MyMainWindow::showEvent(QShowEvent *event)
{
    QMainWindow::showEvent(event);
    QString file_path = windowFilePath();
    setWindowFilePath(file_path+"wtf we have some random text here");
    setWindowFilePath(file_path);
}
/******************************************************************************/

It will just set title to value that you used before widget show (in constructor, in your case). Works like a charm.

極樂鬼 2024-09-21 17:01:38

由于某种原因,从 QMainWindow 的构造函数调用时,setWindowFilePath() 似乎不起作用。但您可以使用单次计时器:

class mainwin : public QMainWindow
{
...
private slots:
    void setTitle();
}

mainwin::mainwin(QWidget *parent) :
    QMainWindow(parent)
{
    QTimer::singleShot(0, this, SLOT(setTitle()));
}

void mainwin::setTitle()
{
    setWindowFilePath("C:\\asdf.txt");
}

并记住在文字路径中使用 \\ 而不是 \

For some reason, setWindowFilePath() does not seem to work when called from QMainWindow's constructor. But you can use single shot timer:

class mainwin : public QMainWindow
{
...
private slots:
    void setTitle();
}

mainwin::mainwin(QWidget *parent) :
    QMainWindow(parent)
{
    QTimer::singleShot(0, this, SLOT(setTitle()));
}

void mainwin::setTitle()
{
    setWindowFilePath("C:\\asdf.txt");
}

And remember to use \\ in literal paths instead of \

蓝天 2024-09-21 17:01:38

我刚刚发现使用QTimer::singleShot,显然没有办法传递参数。要传递参数(在我的例子中,是使用 QSettings 检索的文件路径),请使用:

QMetaObject::invokeMethod(this, "Open", Qt::QueuedConnection, Q_ARG(QString, last_path));

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:

QMetaObject::invokeMethod(this, "Open", Qt::QueuedConnection, Q_ARG(QString, last_path));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文