2

我似乎无法让 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");

}
4

3 回答 3

2

它是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);
}
/******************************************************************************/

它只会将标题设置为您在小部件显示之前使用的值(在构造函数中,在您的情况下)。奇迹般有效。

于 2011-04-26T22:48:42.433 回答
1

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

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");
}

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

于 2010-08-16T17:19:12.013 回答
0

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

QMetaObject::invokeMethod(this, "Open", Qt::QueuedConnection, Q_ARG(QString, last_path));
于 2010-08-16T18:55:58.790 回答