View Full Version : Main Window initialisation
Hi,
My gui has 3 buttons for selecting a directory if required, a button for opening data files and a button for saving a text files (all opening a Qfiledialog dialog).
During the main window initialization I want to change the label of the directory selecting button to the application starting directory (code below)
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDir CurrentDir;
ui->m_Button_Select_Directory->setText(CurrentDir.currentPath());
}
The gui starts with the label changed to the gui starting directory.
My problem is when I clicked either the opening or saving files button the directory picking button is reset to its design value.
Clicking on any other buttons/tabs has not effect. Could it be a Qfiledialog problem?
Can someone point out to the possible problem here?
stampede
4th July 2011, 13:00
Why do you think that opening a file dialog will change the button label in your custom ui ? It's more probable that you have setText() call somewhere else in your code.
Also I noticed that if I change the mainwindow title it is reset to "mainwindow" if I clicked any of the 3 buttons openinf a Qfiledialog dialog.
FelixB
4th July 2011, 13:43
then something is wrong with the slot you're calling when clicking the button. you probably re-initialize your gui.
Hi,
I have done some test and it is the use of Qfiledialog static functions: getExistingDirectory
getOpenFileName
getSaveFileName
that seems to trigger once a reset of the gui. What's weird is that it happens only once.
the slots seems fine if I remove the call to Qfiledialog static functions getExistingDirectory, getOpenFileName or getSaveFileName nothing happens.
Why do you think that opening a file dialog will change the button label in your custom ui ? It's more probable that you have setText() call somewhere else in your code.
I checked all my settext and none would explain this behaviour. I also noticed that the gui title is rest to "mainwindow"
FelixB
4th July 2011, 14:44
show relevant code, please...
Hi,
So to avoid confusion and to make it simple I created a simple example with 2 buttons and that's all. The code was first created by QT creator (QT4 gui application) all default settings. Then 2 buttons were added to the gui with qt designer. Finally one slot created in creator using the option "go to slot".
here is main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->pushButton->setText("I changed the text");
setWindowTitle("This is the title I would like to be");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_pushButton_2_clicked()
{
QFileDialog mydialog;
QString fn = mydialog.getOpenFileName(this, tr("Open File"),"/home",tr("Images (*.png *.xpm *.jpg)"));
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
private slots:
void on_pushButton_2_clicked();
};
#endif // MAINWINDOW_H
And here is what's happening: when selecting the second button opening a qfiledialogue using static function getopenfilename the label of the first button and the window title are reset (using dynamic qfiledialog does the same as far as I can tell).
Either there is something I am missing due to my lack of knowledge or this is a bug.
stampede
4th July 2011, 20:21
Can you prepare and attach compilable example (I mean all needed files, .cpp, .h and .ui) so we can test it ?
edit:
and can you verify that LanguageChange event is not triggered ? I think all strings will be replaced to defaults set in designer if you call ui->retranslateUi(), you should place all setText(), setTitle() etc. code to be called in event handler in case of LanguageChange event.
Hi,
Here are the files.
Yes the retranslate is the problem. This is automatically generated by Qt creator wizard and it did not bother me until now.
I am not quite sure yet to understand the way that this language event loop works or the possible benefits for my gui. My gui is intended to a scientific audience who use english as main working/communication language so I would comment out this retranslate command for the time being.
stampede
5th July 2011, 10:29
You don't have to comment out anything, just set new strings and window titles in event handler during LanguageChange event:
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
this->retranslate();
break;
default:
break;
}
}
void MainWindow::retranslate(){
ui->retranslateUi(this);
ui->pushButton->setText(tr("I changed the text"));
setWindowTitle(tr("This is the title I would like to be"));
}
In constructor call this->retranslate() and its done.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.