PDA

View Full Version : setWindowFilePath seems doesn't work



borisbn
15th June 2011, 14:04
Hi !
I made a simple project with a simple MainWindow:


MainWindow::MainWindow( QWidget *parent, Qt::WFlags flags )
: QMainWindow( parent, flags )
{
ui.setupUi( this );

setWindowTitle( "My Program Caption - " );
setWindowFilePath( "filename.ext" );
//setWindowModified( true );
}

and window has only "My Program Caption - " in it's title, but no "filename.ext".
It doesn't matter, if I call setWindowModified either with true or false, of if I don't call it at all...

The question is: how can I make a title of my window like this

My Program Caption - filename.ext
?
Of course I can make it "by hands" every time I change filename, but I do not want to keep "clear" title ("My Program Caption - "). I want to call setWindowTitle one time in the constructor.
Thanks.

P.S. Windows 7, MSVC 2008, Qt 4.6.

Lesiok
15th June 2011, 14:50
Use __FILE__ macro :
setWindowTitle(QString("My Program Caption - %1").arg(__FILE__));

wysota
15th June 2011, 15:15
To quote the docs:

If the window title is set at any point, then the window title takes precedence and will be shown instead of the file path string.

borisbn
15th June 2011, 16:08
Use __FILE__ macro
I do not want to include source file to window title. I want to include a filename, that my program works with. For example:
If notepad.exe edits somefile.txt, it's title is "Notepad - somefile.txt". I want the same, but to set an initial title ("Notepad - ") once in window's constructor, and to change only a filename each time user opens file to edit.

To quote the docs:
If the window title is set at any point, then the window title takes precedence and will be shown instead of the file path string.

I know this, but even I removed setWindowTitle and leave only setWindowFilePath, I had only application name in title, but not a filename, I gave with setWindowFilePath.

P.S. I understand, that I can do like this:

void MainWindow::onFileOpen( const QString & filename ) {
setWindowTitle( "My Caption - " + filename );
}
but where is a reason of setWindowFilePath function ???
Thanks.

wysota
15th June 2011, 16:17
I know this, but even I removed setWindowTitle and leave only setWindowFilePath, I had only application name in title, but not a filename, I gave with setWindowFilePath.
Did you remove the call the Ui class generated by Designer/uic does as well?


but where is a reason of setWindowFilePath function ???
The reason is the method is platform agnostic whereas your code is not.

borisbn
15th June 2011, 17:30
Did you remove the call the Ui class generated by Designer/uic does as well?
Yes I did. I cleared a WindowTitle property in a designer, compiled and was convinced, that setWindowTitle was removed from ui_MainWindow.h file.

Finally I've done it. See, if setWindowFilePath is called from constructor title doesn't change because of (Qt sources)

void QWidgetPrivate::setWindowTitle_helper(const QString &title)
{
Q_Q(QWidget);
if (q->testAttribute(Qt::WA_WState_Created))
setWindowTitle_sys(qt_setWindowTitle_helperHelper( title, q));
}

I moved call of setWindowFilePath to showEvent

void MainWindow::showEvent( QShowEvent * event ) {
QWidget::showEvent( event );
static bool first = true;
if ( first ) {
first = false;
setWindowFilePath( m_fileName );
}
}

and it works fine, but... do you know a way to run some code once after Widget's class constructor, but without this ugly static bool ? And without calling singleShot from constructor ?

wysota
15th June 2011, 18:36
single shot is ok, if you don't like it, you can use QMetaObject::invokeMethod().

borisbn
15th June 2011, 19:26
Ok, I trust you about single shot, but I think I'll use invokeMethod, because it can transmitt parameters to my function, such as a filename
Thank you once more