Solved

how can I get setWindowFilePath() to work?

Posted on 2010-08-16
4
871 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

743 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

13 Experts available now in Live!

Get 1:1 Help Now