Solved

how can I get setWindowFilePath() to work?

Posted on 2010-08-16
4
877 Views
Last Modified: 2013-11-23
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");

}

Open in new window

0
Comment
Question by:davidburson
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:irfan_omair
ID: 33447744
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");
0
 

Author Comment

by:davidburson
ID: 33447828
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?
0
 
LVL 7

Expert Comment

by:irfan_omair
ID: 33448009
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";
else
shownName = QFileInfo(fileName).fileName();
setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Rich Text")));
setWindowModified(false);

0
 

Accepted Solution

by:
davidburson earned 0 total points
ID: 33448091
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now