Results 1 to 10 of 10

Thread: Main Window initialisation

  1. #1
    Join Date
    May 2011
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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)
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent), ui(new Ui::MainWindow)
    3. {
    4. ui->setupUi(this);
    5. QDir CurrentDir;
    6. ui->m_Button_Select_Directory->setText(CurrentDir.currentPath());
    7. }
    To copy to clipboard, switch view to plain text mode 
    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?

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Main Window initialisation

    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.

  3. #3
    Join Date
    May 2011
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Main Window initialisation

    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.

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Main Window initialisation

    then something is wrong with the slot you're calling when clicking the button. you probably re-initialize your gui.

  5. #5
    Join Date
    May 2011
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Main Window initialisation

    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.

    Quote Originally Posted by stampede View Post
    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"

  6. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Main Window initialisation

    show relevant code, please...

  7. #7
    Join Date
    May 2011
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Main Window initialisation

    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
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtGui>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. ui->pushButton->setText("I changed the text");
    11. setWindowTitle("This is the title I would like to be");
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::changeEvent(QEvent *e)
    20. {
    21. QMainWindow::changeEvent(e);
    22. switch (e->type()) {
    23. case QEvent::LanguageChange:
    24. ui->retranslateUi(this);
    25. break;
    26. default:
    27. break;
    28. }
    29. }
    30.  
    31. void MainWindow::on_pushButton_2_clicked()
    32. {
    33. QFileDialog mydialog;
    34. QString fn = mydialog.getOpenFileName(this, tr("Open File"),"/home",tr("Images (*.png *.xpm *.jpg)"));
    35. }
    To copy to clipboard, switch view to plain text mode 
    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow {
    11. Q_OBJECT
    12. public:
    13. MainWindow(QWidget *parent = 0);
    14. ~MainWindow();
    15.  
    16. protected:
    17. void changeEvent(QEvent *e);
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21.  
    22. private slots:
    23. void on_pushButton_2_clicked();
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    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.

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Main Window initialisation

    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.
    Last edited by stampede; 4th July 2011 at 19:29.

  9. #9
    Join Date
    May 2011
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Main Window initialisation

    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.
    Attached Files Attached Files

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Main Window initialisation

    You don't have to comment out anything, just set new strings and window titles in event handler during LanguageChange event:
    Qt Code:
    1. void MainWindow::changeEvent(QEvent *e)
    2. {
    3. QMainWindow::changeEvent(e);
    4. switch (e->type()) {
    5. case QEvent::LanguageChange:
    6. this->retranslate();
    7. break;
    8. default:
    9. break;
    10. }
    11. }
    12.  
    13. void MainWindow::retranslate(){
    14. ui->retranslateUi(this);
    15. ui->pushButton->setText(tr("I changed the text"));
    16. setWindowTitle(tr("This is the title I would like to be"));
    17. }
    To copy to clipboard, switch view to plain text mode 
    In constructor call this->retranslate() and its done.

Similar Threads

  1. Replies: 2
    Last Post: 17th February 2011, 12:30
  2. Replies: 3
    Last Post: 23rd December 2010, 06:55
  3. Replies: 9
    Last Post: 16th May 2010, 16:21
  4. Replies: 11
    Last Post: 11th August 2008, 09:14
  5. Replies: 15
    Last Post: 23rd March 2007, 16:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.