how can I get setWindowFilePath() to work?

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
    explicit mainwin(QWidget *parent = 0);


public slots:


#endif // MAINWIN_H

#include "mainwin.h"
#include <QApplication>

int main(int argc, char *argv[])
    QApplication app(argc, argv);
    app.setApplicationName("my test");
    mainwin window;;
    return app.exec();

#include "mainwin.h"

mainwin::mainwin(QWidget *parent) :


Open in new window

Who is Participating?
davidbursonConnect With a Mentor Author Commented:
It appears from further research that setWindowFilePath() doesn't work when called from the constructor.  Instead, you have to use QTimer::singleShot to call a slot.  In the slot, you can use setWindowFilePath.
I dont see any problem with this code. But can you check with backward slash and forward slash as Qt takes it different and it converts forward slash to unix standard .
OR, can you try putting double slash like  setWindowFilePath("C:\\asdf.txt");
OR  setWindowFilePath("C:/asdf.txt");
davidbursonAuthor Commented:
none of those worked.  Is there some setting somewhere that needs to be activated, or something like that?

The value does get set, it just isn't being displayed in the title bar.  For example, I can add:
qDebug() << windowFilePath();

(and #include <QDebug> at the top of mainwin.h)

and whatever I pass into setWindowFilePath is printed out in my Application Output window.  So it is setting the value.  But what do I have to do to get it to display the value in the title bar?
Can you check is that file name which you are supplying really exists in that path?
sounds strange if it exists and still that does not work, can you try adding  this setWindowModified(false);
If this does not work too, then I will say add manual code which does this
QString shownName;
if (fileName.isEmpty())
shownName = "untitled.txt";
shownName = QFileInfo(fileName).fileName();
setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Rich Text")));

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.