2010-08-16 61 views
1

我似乎无法让setWindowFilePath在我的任何项目中工作。该值存储并可以检索,但它永远不会显示在我的应用程序的标题栏中。它在我下载的示例应用程序中正常工作,但我找不到他们做的不同。无论如何,这是我创建的一个简单的应用程序来演示问题。我从下面的3个文件mainwin.h,main.cpp和mainwin.cpp中粘贴了代码。如何使用Qt setWindowFilePath

任何想法?我在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"); 

} 

回答

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

记住在字面路径使用\

+0

谢谢,做到了!并感谢提醒关于\\而不是\ – 2010-08-16 17:52:19

0

我刚刚发现\\而不是与QTimer :: singleShot,显然是没有办法来传递参数。要传递参数(在我的情况下,使用QSettings检索文件路径),请使用:

QMetaObject::invokeMethod(this, "Open", Qt::QueuedConnection, Q_ARG(QString, last_path)); 
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); 
} 
/******************************************************************************/ 

它只是将设置标题值在你面前展示窗口小部件使用(在构造函数中,你的情况)。奇迹般有效。

+0

似乎这个错误是固定的。至少我不能在Qt 5.0.2中重现它。 – 2013-05-27 17:11:47