read state of checkbox within qTable



  • 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?



  • @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?



  • @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?



  • @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();
    

  • Qt Champions 2017

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



  • @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 ... ;-)


  • Qt Champions 2017

    @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.



  • @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 :)


  • Qt Champions 2017

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



  • @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?



  • 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)
    


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.