Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-71366

Qt Designer: Need to be able to drag widgets into a QMainWindow's toolbar

XMLWordPrintable

    • Icon: Task Task
    • Resolution: Unresolved
    • Icon: P2: Important P2: Important
    • None
    • 4.8.7, 5.0.0, 5.12
    • Tools: Designer
    • None

      So far in Designer, it seems the only thing you can drag and drop into QToolBar is a QAction. That's very limiting! But QToolBar::addWidget() exists, so I think it should be possible to drag other types of widgets into it. The most common ones people might want to add would be QToolButton (obviously, although it's redundant since dragging a QAction there will create one anyway); QLineEdit (for a search field), QSpinBox (for page numbers in a PDF viewer, or any other number that has significance for the whole window), QLabel (because QSpinBox doesn't include one), QCheckBox, QFontComboBox, QSlider... basically anything that is compact enough to fit.

      The workaround is a kind of transplant operation I figured out while creating a presentation for QtWS:

      • Add some sort of horizontal toolbar-like bar to the top of the content area (a QHBoxLayout is fine)
      • Add widgets-for-the-toolbar to that
      • In C++ setup code (e.g. MainWindow subclass constructor): loop over the widgets in that temporary container, call addWidget() to add each one to the toolbar, then delete the temporary container now that it's no longer needed:
            while (ui->toolbarStuff->count()) {
                QWidget *tw = ui->toolbarStuff->takeAt(0)->widget();
                if (!tw)
                    continue;
                tw->setAttribute(Qt::WA_AcceptTouchEvents);
                ui->mainToolBar->addWidget(tw);
            }
            delete ui->toolbarStuff;
        

      Another advantage of this workaround is it's possible to connect signals and slots between those toolbar-destined widgets and the widgets on the main form that they manipulate. But I actually expect to be able to connect signals from widgets that are already on the toolbar to slots on any other object that is visible in the designer form.

      Another issue that will come up is how to mix widgets and actions and separators, and have control over where they appear. There are limitations even in C++ because of a couple missing functions like QAction *insertSeparator(QWidget *before) and insertWidget(QWidget *before, QWidget *widget). Although not very bad limitations, because you can insert a widget before an action, and you can add a widget at the end, and you can add an action at the end, so it's about enough to implement the DnD I think.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            srutledg Shawn Rutledge
            srutledg Shawn Rutledge
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes