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. QTableWidget: disabling automatic selection of row header when a column is selected and viceversa
Forum Updated to NodeBB v4.3 + New Features

QTableWidget: disabling automatic selection of row header when a column is selected and viceversa

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 3.6k Views
  • 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.
  • B Offline
    B Offline
    Black Imp
    wrote on last edited by
    #1

    I 've searched everywhere I could before posting again. Probably I use the wrong key words:
    the default behaviour I've seen so far is that when you select a row header, all column sections in column header get selected too. Viceversa if you select a column by pressing its header section all row sections get selected.
    I'd need to avoid this behaviour and having just one row section selection when I select one row and only one column section when I select one column.
    Is it possible?

    these picture show the behaviour I dont want.

    I'd like only column 2 selected withou having row sections "One" "Two" "Three" selected.

    table col selection.png

    similarly here...

    table row selection.png

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Connect to QHeaderView::sectionSelected() or sectionPressed() and do the selection you want.
      It may also work when you override QTableView::selectColumn() and QTableView::selectRow()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      B 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Connect to QHeaderView::sectionSelected() or sectionPressed() and do the selection you want.
        It may also work when you override QTableView::selectColumn() and QTableView::selectRow()

        B Offline
        B Offline
        Black Imp
        wrote on last edited by Black Imp
        #3

        @Christian-Ehrlicher thank you:

        I can't override those methods as they are not "virtual".

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            a.setStyleSheet(
                    "QTableWidget { background-color: #000000; alternate-background-color: #222222; gridline-color: #AAAAAA; color: #FFFFFF; selection-background-color: #FFC000; selection-color: #AA00FF}"
                    "QTableWidget QHeaderView { background-color: #00000000;}"
                    "QTableWidget QHeaderView::section { background-color: #444444; color: #FFFFFF; border: none; border-style: none;}"
                    "QTableWidget QHeaderView::section:checked { background-color: #FFC000; color: #000000;}"
                    "QHeaderView::section:horizontal { border-right: 1px solid #AAAAAA; }"
                    "QHeaderView::section:vertical { border-bottom: 1px solid #AAAAAA; }"
                    "QTableWidget QTableCornerButton::section {  background: #00000000;  border: 0px   }"
                            );
        
        
            MainWindow w;
            w.show();
            return a.exec();
        }
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        protected slots:
            void onColumnSelected(int);
        private:
            QMdiArea * mArea;
        };
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
        {
            mArea = new QMdiArea();
            setCentralWidget(mArea);
        
            QMdiSubWindow * sw = new QMdiSubWindow();
            sw->resize(900, 600);
            mArea->addSubWindow(sw);
            sw->show();
        
            QWidget * widget = new QWidget();
            QVBoxLayout * vlayout = new QVBoxLayout();
            widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            widget->setLayout(vlayout);
        
            sw->setWidget(widget);
        
            QTableWidget * table = new QTableWidget();
            table->setAlternatingRowColors(true);
            QStringList verHeaderList({"One", "Two", "Three"});
            table->setRowCount(verHeaderList.size());
            table->setColumnCount(5);
           
            table->setVerticalHeaderLabels(verHeaderList);
            table->verticalHeader()->setVisible(true);
            
            connect(table->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onColumnSelected(int)));
            
            table->show();
        
            vlayout->addWidget(table);
        }
        
        void MainWindow::onColumnSelected(int index)
        {
            qDebug() << __FUNCTION__ << "index " << index;
        }
        

        onColumnSelected works correctly.
        Issues:

        1. how can I stop table from highlighting-applying selection-changing color of cells and header sections automatically
        2. how can I select-change color of cells and header section clicked in my onColumnSelected

        if I insert in MainWindow constructor this test code:

        table->setItemSelected( table->verticalHeaderItem(1), true);
        

        I see no change at all.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Black Imp
          wrote on last edited by
          #4

          ok I 've found some solutions thou I have more problems

          disconnect(table->horizontalHeader(), SIGNAL(sectionPressed(int)),table, SLOT(selectColumn(int)));
          disconnect(table->verticalHeader(), SIGNAL(sectionPressed(int)),table, SLOT(selectRow(int)));
          

          then I reconnect to my slots which select no more than one row or column

          connect(table->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onColumnSelected(int)));
          connect(table->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onRowSelected(int)));
          connect(table->horizontalHeader(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(onColumnDoubleClicked(int)));
          connect(table->verticalHeader(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(onRowDoubleClicked(int)));
          
          void MainWindow::onColumnSelected(int index)
          {
              qDebug() << __FUNCTION__ << "index " << index;
              table->selectColumn(index);
          
          }
          
          void MainWindow::onColumnDoubleClicked(int index)
          {
              qDebug() << __FUNCTION__ << "index " << index;
          }
          
          void MainWindow::onRowSelected(int index)
          {
              qDebug() << __FUNCTION__ << "index " << index;
              table->selectRow(index);
          }
          
          void MainWindow::onRowDoubleClicked(int index)
          {
              qDebug() << __FUNCTION__ << "index " << index;
          }
          
          void MainWindow::onCellSelected(int r, int c)
          {
              qDebug() << __FUNCTION__ << "row index " << r << " column index " << c;
          }
          
          void MainWindow::onCDoubleClicked(int row, int col)
          {
              qDebug() << __FUNCTION__ << "row index " << row << " column index " << col;
          }
          

          I didn't find a way to disconnect single cell pressed thou.

          JonBJ 1 Reply Last reply
          0
          • B Black Imp

            ok I 've found some solutions thou I have more problems

            disconnect(table->horizontalHeader(), SIGNAL(sectionPressed(int)),table, SLOT(selectColumn(int)));
            disconnect(table->verticalHeader(), SIGNAL(sectionPressed(int)),table, SLOT(selectRow(int)));
            

            then I reconnect to my slots which select no more than one row or column

            connect(table->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onColumnSelected(int)));
            connect(table->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(onRowSelected(int)));
            connect(table->horizontalHeader(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(onColumnDoubleClicked(int)));
            connect(table->verticalHeader(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(onRowDoubleClicked(int)));
            
            void MainWindow::onColumnSelected(int index)
            {
                qDebug() << __FUNCTION__ << "index " << index;
                table->selectColumn(index);
            
            }
            
            void MainWindow::onColumnDoubleClicked(int index)
            {
                qDebug() << __FUNCTION__ << "index " << index;
            }
            
            void MainWindow::onRowSelected(int index)
            {
                qDebug() << __FUNCTION__ << "index " << index;
                table->selectRow(index);
            }
            
            void MainWindow::onRowDoubleClicked(int index)
            {
                qDebug() << __FUNCTION__ << "index " << index;
            }
            
            void MainWindow::onCellSelected(int r, int c)
            {
                qDebug() << __FUNCTION__ << "row index " << r << " column index " << c;
            }
            
            void MainWindow::onCDoubleClicked(int row, int col)
            {
                qDebug() << __FUNCTION__ << "row index " << row << " column index " << col;
            }
            

            I didn't find a way to disconnect single cell pressed thou.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Black-Imp
            There are signals like cellClicked & cellPressed, though I don't see you connecting those signals to your slots.

            What do you mean by/why do you want to "disconnect single cell pressed"? Signals are disconnected in the normal way, via QObject::disconnect(). The signal is for the table as a whole, so you can't connect/disconnect individual cells. However, in your slot you can handle only those you wish to. Though I suspect you already know that, so not sure what you have in mind.

            B 1 Reply Last reply
            0
            • JonBJ JonB

              @Black-Imp
              There are signals like cellClicked & cellPressed, though I don't see you connecting those signals to your slots.

              What do you mean by/why do you want to "disconnect single cell pressed"? Signals are disconnected in the normal way, via QObject::disconnect(). The signal is for the table as a whole, so you can't connect/disconnect individual cells. However, in your slot you can handle only those you wish to. Though I suspect you already know that, so not sure what you have in mind.

              B Offline
              B Offline
              Black Imp
              wrote on last edited by Black Imp
              #6

              @JonB I'd need to disconnect both headers and cells from any slot which automatically higlights them.

              My desired behaviour would be:

              i click on a cell, it highlights and it becomes selected.

              i click on a header section - row or column - and it highlights with all the cells on that row or on that column.

              I don't want more than a row/column selected, I don't want more than a single cell selected unless I selected a row or a column.

              I'm trying some solutions but I can't find a good compromised:

              1 . first of all, if I disconnect as I did header selectionPressed from selectColumn/Row I can't find a way to highlight programmatically a header section without selecting it.
              2. If I call, in my own slots connected to those signals , selectColumn(index); I get the good effect which only one row/column can be actually selected thou you can highlight more than one when you keep mouse button presse and move, but to have this behaviour you must keep table with a selection mode which allows for multiple selection. this means you cannot call setSelectionMode(QAbstractItemView::SingleSelection);
              3. If I don't call setSelectionMode(QAbstractItemView::SingleSelection); table allows for multiple cell selection by clicking on different cells. I only want multiple selection by clicking headers. I've tried all MultiSelection, ExtendedSelection, ContiguousSelection but none of them allow me to do what I need and user is able to make a mess like this:
              undesired multiple selection.png
              4. I could then try to disconnect some signal which automatically highlights cells when clicking over them and try to highlight them programmatically. I can't understand what slot I should disconnect and how to manually higlight cells.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Black Imp
                wrote on last edited by
                #7

                please you can close this thread. I've found a possible solution to my needs but I just have to solve a little last issue which I can't understand how to solve. I'd post a new thread with that question only, if possible

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Black-Imp said in QTableWidget: disabling automatic selection of row header when a column is selected and viceversa:

                  please you can close this thread

                  Only you can do this - mark it as solved with the topic Tools.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  B 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @Black-Imp said in QTableWidget: disabling automatic selection of row header when a column is selected and viceversa:

                    please you can close this thread

                    Only you can do this - mark it as solved with the topic Tools.

                    B Offline
                    B Offline
                    Black Imp
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher ok thank you. I'll delete all and open a new one

                    JonBJ 1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Black-Imp said in QTableWidget: disabling automatic selection of row header when a column is selected and viceversa:

                      I'll delete all and open a new one

                      ?

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0
                      • B Black Imp

                        @Christian-Ehrlicher ok thank you. I'll delete all and open a new one

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @Black-Imp
                        Please don't delete your topic, there is nothing wrong with your question and the discussions can always be useful to others reading it, even if you have found another approach now.

                        At the bottom of the page you should see a Topic Tools button (because this is your topic), pick Mark as Solved from there. If you choose to open a new topic now, you might like to put in a one-line reference to this topic from there so people know there was a relationship.

                        B 1 Reply Last reply
                        4
                        • JonBJ JonB

                          @Black-Imp
                          Please don't delete your topic, there is nothing wrong with your question and the discussions can always be useful to others reading it, even if you have found another approach now.

                          At the bottom of the page you should see a Topic Tools button (because this is your topic), pick Mark as Solved from there. If you choose to open a new topic now, you might like to put in a one-line reference to this topic from there so people know there was a relationship.

                          B Offline
                          B Offline
                          Black Imp
                          wrote on last edited by
                          #12

                          @JonB ok thou there's nothing solved here

                          jsulmJ 1 Reply Last reply
                          0
                          • B Black Imp

                            @JonB ok thou there's nothing solved here

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @Black-Imp You still have problems?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            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