2010-08-16 66 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