Results 1 to 1 of 1

Thread: Changing active window in my SDI application doesn't always work.

  1. #1
    Join Date
    Mar 2011
    Location
    Madrid
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Changing active window in my SDI application doesn't always work.

    Howdy,

    In this exciting process of learning Qt, I am trying to change the "basic"-part example from the "C++ GUI Programming with Qt 4" book (chapters 1--4) from a one window app to a multiple window one. The application is a (very basic) spreadsheet.

    The class MainWindow inherits from QMainWindow; each instance is a single document window. The class Spreadsheet inherits from QTableWidget and each MainWindow has a Spreadsheet as a Central Widget. I have this working pretty well, except when I want to activate a window (one which is not currently active) in the following situation: when the user, from the currently active window, chooses to open a document which is open in a different window. The code I wrote to achive this is:

    Qt Code:
    1. bool MainWindow::loadFile(const QString &fileName)
    2. {
    3. if (!raiseWhenOpened(fileName)) {
    4. if (!spreadsheet->readFile(fileName)) {
    5. statusBar()->showMessage(tr("Loading canceled"), 2000);
    6. return false;
    7. }
    8.  
    9. setCurrentFile(fileName);
    10. statusBar()->showMessage(tr("File loaded"), 2000);
    11. return true;
    12. }
    13. return false;
    14. }
    15.  
    16. bool MainWindow::raiseWhenOpened(const QString &fileName)
    17. {
    18. foreach (QWidget *wid, QApplication::topLevelWidgets()) {
    19. if (MainWindow *mainWin = qobject_cast<MainWindow *>(wid)) {
    20. if (mainWin->curFile == fileName) {
    21. QMessageBox::information(this, tr("Spreadsheet"),
    22. "The file \""+ strippedName(fileName) +
    23. "\" is already open in another window.");
    24. mainWin->raise();
    25. mainWin->show();
    26. mainWin->activateWindow();
    27. mainWin->setFocus();
    28. return true;
    29. }
    30. }
    31. }
    32. return false;
    33. }
    To copy to clipboard, switch view to plain text mode 

    curFile is a QString type member of MainWindow holding the name of the file opened in that window. I put the QMessageBox there mainly as a breakpoint and to check that mainWin is pointing to the right thing (which it is). I've tried several combinations of the mainWin function calls raise(), show(), activateWindow() and setFocus(). The current behavior is that it makes mainWin be on top of the currently active window (this), but it does not make mainWin active (it doesn't have the focus).

    The odd thing is that when I create a new window (from the file->new menu or Ctrl+N) this code
    Qt Code:
    1. void MainWindow::newFile()
    2. {
    3. if (okToContinue()) {
    4. MainWindow *mainWin = new MainWindow;
    5. mainWin->show();
    6. }
    To copy to clipboard, switch view to plain text mode 
    does work and the new window has the focus (okToContinue checks whether the user wants to save changed documents, etc.).

    I've searched for problems using activateWindow() and I found several issues, but I believe none of them in this situation. Since I am new to Qt, there's a high probability that I am doing something silly, and I'd be much obliged for any help.

    I am runnig Qt 4.6.2 on Ubuntu 10.04 under Gnome.

    Cheers, Manu
    Last edited by portilhe; 4th March 2011 at 19:23.

Similar Threads

  1. Slot doesn't work in new window
    By mohjlir in forum Newbie
    Replies: 2
    Last Post: 27th January 2011, 10:50
  2. Replies: 1
    Last Post: 17th December 2010, 08:53
  3. Replies: 2
    Last Post: 29th June 2010, 00:40
  4. How to build an application to work as a Window Service
    By danielperaza in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2008, 23:54
  5. how to make Window+M key work for my application
    By yxmaomao in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2008, 00:35

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.