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. Settings widget into focus programmatically.
Forum Updated to NodeBB v4.3 + New Features

Settings widget into focus programmatically.

Scheduled Pinned Locked Moved Unsolved General and Desktop
qwidgetqcomboboxqtreeview
9 Posts 3 Posters 2.1k Views 2 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    I have custom QComboBox with QTreeView that I would like to show when user press on the drop down arrow to show combo options.

    I replaced QComboBox::showPopup call with me showing the tree in the correct place location.

    I also set these parameters on the tree :

        mView_tree->setFocusPolicy(Qt::StrongFocus);
        mView_tree->setFrameShape(QFrame::NoFrame);
        mView_tree->setWindowFlags(Qt::Tool | Qt::CustomizeWindowHint);
    

    And I showPopup :

    ... some code
    mView_tree->move(loc);
    mView_tree->show();
    mView_tree->setFocus(Qt::MouseFocusReason);
    

    When the tree shows up, it does not have focus. How can I get it to focus properly?

    I also tried this in case some QComboBox signals break stuff - at the bottom of showPopup func.

        QMetaObject::invokeMethod(mView_tree, [=]() { mView_tree->setFocus(); }, Qt::QueuedConnection);
    

    Sadly no luck, any info/help?

    TIA

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      But who has focus then?
      Try use
      QApplication::focusWidget()
      to see what widget steals your focus.

      1 Reply Last reply
      2
      • D Offline
        D Offline
        Dariusz
        wrote on last edited by
        #3

        Thats a brilliant thing!

        Its the evil combo box ! -.-

        What can I do to drop its focus? I'm lost :- (

        mrjjM 1 Reply Last reply
        0
        • D Dariusz

          Thats a brilliant thing!

          Its the evil combo box ! -.-

          What can I do to drop its focus? I'm lost :- (

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Dariusz
          The first test might be to
          combo->setFocusPolicy(Qt::NoFocus);

          1 Reply Last reply
          1
          • D Offline
            D Offline
            Dariusz
            wrote on last edited by
            #5

            @mrjj said in Settings widget into focus programmatically.:

            combo->setFocusPolicy(Qt::NoFocus);

            Nope still in focus :- (

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              How are you creating that special QComboBox ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                How are you creating that special QComboBox ?

                D Offline
                D Offline
                Dariusz
                wrote on last edited by
                #7

                @SGaist
                Hey
                Goes pretty much like this now that I look at it :
                combo is placed inside QWidget that is being used as a setTitleBarWidget(menuBarWidget);

                #include "QComboBox"
                #include "QTreeView"
                
                class comboExample :public QComboBox{
                    Q_OBJECT
                    QTreeView *view;
                public:
                    comboExample();
                    ~comboExample();
                
                    void showPopup();
                    void setModel(QAbstractItemModel*model);
                
                };
                
                
                #include "comboExample.h"
                #include "QHeaderView"
                
                comboExample::comboExample() {
                    view = new QTreeView();
                    view->show();
                    view->setStyleSheet("border: 1px solid gray");
                    view->header()->setHidden(true);
                    view->setFrameShape(QFrame::NoFrame);
                    view->setEditTriggers(QTreeView::NoEditTriggers);
                    view->setAlternatingRowColors(true);
                    view->setSelectionBehavior(QTreeView::SelectRows);
                    view->setWordWrap(true);
                    view->setAllColumnsShowFocus(true);
                    view->setWindowFlags(Qt::Tool | Qt::CustomizeWindowHint);
                   setMaximumWidth(100);
                   setAttribute(Qt::WA_TranslucentBackground, true);
                   setMaximumHeight(22);
                }
                comboExample::~comboExample() {
                
                }
                void comboExample::showPopup() {
                    //QComboBox::showPopup();
                    view->move(pos());///wrong pos ...
                    view->show();
                    view->setFocus(Qt::MouseFocusReason);
                }
                void comboExample::setModel(QAbstractItemModel *model) {
                    view->setModel(model);
                }
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  This Wiki article shows a starting point to get a tree view inside a QComboBox. It needs some cleanup but should get you started.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3
                  • D Offline
                    D Offline
                    Dariusz
                    wrote on last edited by
                    #9

                    That article answers quite a few questions I had about that scenario, woah nice thanks!

                    But... I kinda like the idea of more "control"... somehow, creating the widget this way allow the user to resize it which won't be possible using a popup, so I would like to get the focus to work properly...

                    TIA

                    1 Reply Last reply
                    0

                    • Login

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