Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QMdiArea::addSubWindow() issue
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QMdiArea::addSubWindow() issue

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 8.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Seba84
    wrote on last edited by
    #1

    Hi everyone,

    It is the first time I'm using MdiArea object and to test it I did a little program.

    I created a MainWindow with a QMdiArea as central widget, a toolbox and a statusbar. In the toolbox there is a new button that should open a new sub window, inside the sub window there is only a QPushButton. When I click on the "New" button (actionNew) the confirmation message appears on the statusbar but no sub window is opened. I searched on the qt help and on the net, I did several changes on the code but all without success.

    Below the code:

    @class MainWindow : public QMainWindow {
    Q_OBJECT
    Ui::MainWindow *ui;

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    public slots:
    void _add_subwin();
    void statusbar_msg(const QString &);
    };

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    // SETUP USER-INTERFACE
    ui->setupUi(this);
    // ADDS A SUB-WINDOW (NOT WANTED!!)
    QMdiSubWindow *subwin1 = ui->mdiArea->addSubWindow(new QPushButton(this));
    subwin1->setAttribute(Qt::WA_DeleteOnClose);

    // CONNECTIONS
    connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(_add_subwin()));
    }

    MainWindow::~MainWindow() {
    ui->mdiArea->closeAllSubWindows();
    delete ui;
    }

    void MainWindow::_add_subwin() {
    // ADDS THE SUB-WINDOW
    QMdiSubWindow *subwin1 = ui->mdiArea->addSubWindow(new QPushButton(this));
    subwin1->setAttribute(Qt::WA_DeleteOnClose);
    //subwin->show();
    //ui->mdiArea->update();

    // STATUSBAR MESSAGE
    statusbar_msg("New window added");
    }

    void MainWindow::statusbar_msg(const QString &msg) {
    // SENDS TO THE STATUS BAR A MESSAGE (TIMEOUT = 5 seconds)
    ui->statusBar->showMessage(msg, 5000);
    }@

    As you can see, the only way I managed to open a subwindow is from the constructor (not the behavior wanted).
    I tried QMdiSubWindow::show() and QMdiArea::update() from the MainWindow::_add_subwin() function but doesn't works.

    Thanks in advance for your help.
    Sebastian

    1 Reply Last reply
    0
    • J Offline
      J Offline
      joeuser
      wrote on last edited by
      #2

      I've had focusing issues when opening multiple subwindows unless I create them in the following manner:

      @
      QMdiSubWindow* sw = new QMdiSubWindow( ui->mdiArea );
      sw->setWidget( new QPushButton( sw ) );
      sw->setAttribute( Qt::WA_DeleteOnClose );
      ui->mdiArea->addSubWindow( sw );
      sw->show();
      @

      perhaps the above pattern could work for you.

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Seba84
        wrote on last edited by
        #3

        Hello joeuser,

        Thanks, this solved the problem! But I would like to understand why.

        From the code above, the reasons I see are:

        1. that I didn't declare as parent of the subwindow the mdiArea object (tested: not true)
        2. that the call of show() is mandatory (tested: true)

        But, in the original code, I called show and nothing happend...

        Anyone has an idea what the problem was?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Seba84
          wrote on last edited by
          #4

          The problem was show(). I tested the old code with show() and now it works:

          @QMdiSubWindow *subwin1 = ui->mdiArea->addSubWindow(new QPushButton);
          subwin1->setAttribute(Qt::WA_DeleteOnClose);
          subwin1->show();@

          Thanks again joeuser.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved