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. read state of checkbox within qTable
Qt 6.11 is out! See what's new in the release blog

read state of checkbox within qTable

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 4.7k 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.
  • L Offline
    L Offline
    Leopold
    wrote on last edited by VRonin
    #1

    Hello,
    I established a table

    void MainWindow::setuptable_list(QTableWidget *table_list)
    {
     //    m_ui->table_list = new QTableWidget(this);
        
    m_ui->table_list->setRowCount(7);
    m_ui->table_list->setColumnCount(6);
    m_ui->table_list->setShowGrid(true);
    m_ui->table_list->setStyleSheet("QTableView {selection-background-color: red;}");
    m_ui->table_list->setSelectionMode(QAbstractItemView::SingleSelection);
    m_ui->table_list->setCellWidget(0, 0, new QCheckBox("1 rpm"));
    m_ui->table_list->setCellWidget(1, 0, new QCheckBox("54  rpm<1500"));
    m_ui->table_list->setCellWidget(2, 0, new QCheckBox("2  watertemp"));
    

    Then I connected

    connect( m_ui->table_list, SIGNAL(  cellChanged(int,int)) ,this, SLOT( cellSelectedSlot(int,int)));
    
    

    and can connect, now I want to know if the checkbox is enabled or not here my code

    void MainWindow::cellSelectedSlot(int nRow,int nCol)
    {
    
        int m=0;
    int Reihe=nRow ;
    int Spalte=nCol;
     qDebug()<<Reihe <<Spalte;
      if ((Reihe== 0)&&(Spalte==0))
            {if (m_ui->table_list->cellWidget(Reihe,Spalte)->QWidget(CheckBox)->isChecked(true))
          {
    

    This doesn' t work, how can I get the checkbox state?

    JonBJ 1 Reply Last reply
    0
    • L Leopold

      Hello,
      I established a table

      void MainWindow::setuptable_list(QTableWidget *table_list)
      {
       //    m_ui->table_list = new QTableWidget(this);
          
      m_ui->table_list->setRowCount(7);
      m_ui->table_list->setColumnCount(6);
      m_ui->table_list->setShowGrid(true);
      m_ui->table_list->setStyleSheet("QTableView {selection-background-color: red;}");
      m_ui->table_list->setSelectionMode(QAbstractItemView::SingleSelection);
      m_ui->table_list->setCellWidget(0, 0, new QCheckBox("1 rpm"));
      m_ui->table_list->setCellWidget(1, 0, new QCheckBox("54  rpm<1500"));
      m_ui->table_list->setCellWidget(2, 0, new QCheckBox("2  watertemp"));
      

      Then I connected

      connect( m_ui->table_list, SIGNAL(  cellChanged(int,int)) ,this, SLOT( cellSelectedSlot(int,int)));
      
      

      and can connect, now I want to know if the checkbox is enabled or not here my code

      void MainWindow::cellSelectedSlot(int nRow,int nCol)
      {
      
          int m=0;
      int Reihe=nRow ;
      int Spalte=nCol;
       qDebug()<<Reihe <<Spalte;
        if ((Reihe== 0)&&(Spalte==0))
              {if (m_ui->table_list->cellWidget(Reihe,Spalte)->QWidget(CheckBox)->isChecked(true))
            {
      

      This doesn' t work, how can I get the checkbox state?

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

      @Leopold said in read state of checkbox within qTable:

      ->QWidget(CheckBox)->

      What does that do? If cellWidget returns the widget, you want something like a dynamic_cast to QCheckBox to access your checkbox, don't you?

      L 1 Reply Last reply
      0
      • JonBJ JonB

        @Leopold said in read state of checkbox within qTable:

        ->QWidget(CheckBox)->

        What does that do? If cellWidget returns the widget, you want something like a dynamic_cast to QCheckBox to access your checkbox, don't you?

        L Offline
        L Offline
        Leopold
        wrote on last edited by
        #3

        @JonB
        I want sth like this: m_ui->table_list->QCheckBox->isChecked(true)) but this didn't work too so I tried the other way.
        I#m out of mind.Can you help?

        JonBJ 1 Reply Last reply
        0
        • L Leopold

          @JonB
          I want sth like this: m_ui->table_list->QCheckBox->isChecked(true)) but this didn't work too so I tried the other way.
          I#m out of mind.Can you help?

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

          @Leopold
          I am not a C++ programmer, so I don't know the exact syntax. But you need (I would have thought) to do something like:

          QCheckBox *cb = dynamic_cast <QCheckBox *> (m_ui->table_list->cellWidget(Reihe,Spalte));
          if (cb)
              isChecked = cb->isChecked();
          
          1 Reply Last reply
          2
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi
            Use qobject_cast instead :)
            Else pretty good hit for non c++ programmer ! ;)

            JonBJ 1 Reply Last reply
            1
            • mrjjM mrjj

              Hi
              Use qobject_cast instead :)
              Else pretty good hit for non c++ programmer ! ;)

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

              @mrjj
              Nowt wrong with dynamic_cast, just a touch slower (and uses RTTI, but so what), and I don't think speed here is going to matter ... ;-)

              mrjjM 1 Reply Last reply
              1
              • JonBJ JonB

                @mrjj
                Nowt wrong with dynamic_cast, just a touch slower (and uses RTTI, but so what), and I don't think speed here is going to matter ... ;-)

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

                @JonB
                well qobject_cast uses Qt meta type system and seems a better fit but
                not sure if any situations where dynamic_cast would do incorrectly vs qobject_cast.
                So nope, wont matter much i guess.

                JonBJ 1 Reply Last reply
                1
                • mrjjM mrjj

                  @JonB
                  well qobject_cast uses Qt meta type system and seems a better fit but
                  not sure if any situations where dynamic_cast would do incorrectly vs qobject_cast.
                  So nope, wont matter much i guess.

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

                  @mrjj
                  Yeah, I was just being flippant, didn't mean to disagree. qobject_cast might be a better "fit", I had assumed OP was familiar with dynamic_cast for other purposes, is all :)

                  mrjjM 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @mrjj
                    Yeah, I was just being flippant, didn't mean to disagree. qobject_cast might be a better "fit", I had assumed OP was familiar with dynamic_cast for other purposes, is all :)

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

                    ah, ok. well actually a good assumption. :)

                    L 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      ah, ok. well actually a good assumption. :)

                      L Offline
                      L Offline
                      Leopold
                      wrote on last edited by
                      #10

                      @all,
                      sorry i can notcome up with that code.With this code , the connect should transport three items int row,intcolumn and bool ischecked.
                      Meanwhile I have found "QChecktableItem" but when I want to include qtable.h, this is not found and QCheckTableItem is also not found. Not either in the help files.Some examples are out of 2003 within QT 3 .I have Qt 5.6, is this QCheckboxItem no longer available.
                      Who has example which works?

                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        There is absolutely no reason to use setCellWidget to set a checkbox as QAbstarctItemModel has that function already implemented:

                        m_ui->table_list->setRowCount(7);
                        m_ui->table_list->setColumnCount(6);
                        m_ui->table_list->setShowGrid(true);
                        m_ui->table_list->setStyleSheet("QTableView {selection-background-color: red;}");
                        m_ui->table_list->setSelectionMode(QAbstractItemView::SingleSelection);
                        QTableWidgetItem* tempItem = new QTableWidgetItem;
                        tempItem->setFlags(tempItem->flags() | Qt::ItemIsUserCheckable);
                        tempItem->setData(Qt::DisplayRole, QStringLiteral("1 rpm"));
                        tempItem->setData(Qt::CheckStateRole, Qt::Unchecked);
                        m_ui->table_list->setItem(0,0,tempItem);
                        tempItem = new QTableWidgetItem;
                        tempItem->setFlags(tempItem->flags() | Qt::ItemIsUserCheckable);
                        tempItem->setData(Qt::DisplayRole, QStringLiteral("54  rpm<1500"));
                        tempItem->setData(Qt::CheckStateRole, Qt::Unchecked);
                        m_ui->table_list->setItem(1,0,tempItem);
                        tempItem = new QTableWidgetItem;
                        tempItem->setFlags(tempItem->flags() | Qt::ItemIsUserCheckable);
                        tempItem->setData(Qt::DisplayRole, QStringLiteral("2  watertemp"));
                        tempItem->setData(Qt::CheckStateRole, Qt::Unchecked);
                        m_ui->table_list->setItem(2,0,tempItem);
                        
                        void MainWindow::cellSelectedSlot(int nRow,int nCol)
                        {
                        
                            int m=0;
                        int Reihe=nRow ;
                        int Spalte=nCol;
                         qDebug()<<Reihe <<Spalte;
                          if ((Reihe== 0)&&(Spalte==0))
                                {if (m_ui->table_list->model()->index(Reihe,Spalte).data(Qt::CheckStateRole)==Qt::Checked)
                        

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        3
                        • L Offline
                          L Offline
                          Leopold
                          wrote on last edited by
                          #12

                          Hello,
                          works perfect .
                          I couldn't have done it myself.
                          Thank you.

                          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