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. Set row highlighted when application start in QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Set row highlighted when application start in QTableWidget

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 9.6k 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.
  • M Offline
    M Offline
    maxmotor
    wrote on last edited by
    #1

    Hello devs!

    I am currently trying to develop an application where I have a QTableWidget.
    This tableWidget has 2 columns (containing text).
    The selection behaviour is set to "QAbstractItemView::SelectRows" - which selects the entire row instead of the single cell.
    Now I'm trying to set the first row as highlighted when my application is launched. I have had no luck so far though.

    Can anyone help me out?

    Thank you for your time.

    1 Reply Last reply
    1
    • T Offline
      T Offline
      task_struct
      wrote on last edited by
      #2

      You can use "void QTableWidget::setCurrentCell ( int row, int column )":http://doc.qt.nokia.com/latest/qtablewidget.html#setCurrentCell with selection mode set to QAbstractItemView::SingleSelection or QAbstractItemView::ExtendedSelection

      "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

      • Linu...
      1 Reply Last reply
      0
      • M Offline
        M Offline
        maxmotor
        wrote on last edited by
        #3

        That doesn't work for me. Guess it has something to do with me selecting an entire row?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          When do you call this method?
          If you do it in the constructor, it will not work, as the input focus has not moved there...

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maxmotor
            wrote on last edited by
            #5

            Yes, I call my QTableWidget setup method from the constructer.

            Here is some of my code:
            @
            Setup::Setup(QWidget *parent) :
            Parent_ui(parent) {

            ui.setupUi(this);
            populateList();
            }

            void Setup::populateList() {

            ui.tableWidget->setColumnCount(2);
            ui.tableWidget->setColumnWidth(0,(ui.tableWidget->width() - 152));
            ui.tableWidget->setColumnWidth(1,150);
            ui.tableWidget->setShowGrid(false);
            ui.tableWidget->verticalHeader()->hide();
            ui.tableWidget->horizontalHeader()->hide();
            ui.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);

            ... //items are added to the QTableWidget
            }
            @

            So I cant call setCurrentCell from my populateList method?

            How do I handle this problem?

            Thank you for your time!

            Edit: Please use @ tags to format code sections; Andre

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              No, you can not. The problem is, that at that time, your form is not even showing yet.
              What you might do, is something like this:

              @
              Setup::Setup(QWidget *parent) :
              Parent_ui(parent) {

              ui.setupUi(this);
              populateList();
              //trigger 2nd phase of initialization when the eventloop is running
              QTimer::singleShot(0, this, SLOT(initializePhase2()));
              }

              void Setup::initializePhase2()
              {
              //set which row to highlight.
              }
              @

              Don't forget to mark initializePhase2 as a (private) slot in your header, and include the Q_OBJECT macro in your class definition, of course. Sorry for the inconvenience...

              1 Reply Last reply
              0
              • M Offline
                M Offline
                maxmotor
                wrote on last edited by
                #7

                bq. Sorry for the inconvenience…

                I am grateful for your help - that goes to all of you. :)

                The cell row is now highlighted grey - what method could set the row as... hmm.. is it called focus? You know like when you click a cell and it turns blue.

                Again - thank you for your time!

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Make the table widget have the focus. See QWidget::setFocus().

                  BTW: note that there is a difference between the active cell and selected cells. They may be rendered differently.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    maxmotor
                    wrote on last edited by
                    #9

                    Oh god... I'm sorry for my ignorance.
                    I answer my own question in my previous post. Sorry.

                    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