[SOLVED] Questions about tablewidget



  • So i got this tablewidget:
    !http://i.imgur.com/nluyyV5.png(1)!

    It adds the same row 2 times.

    Created with this code:
    @for(int i=0; i<2; i++)
    {
    int old_r_count=ui->clocksTableWidget->rowCount();
    ui->clocksTableWidget->insertRow(ui->clocksTableWidget->rowCount());
    QRadioButton *radiobutton_item = new QRadioButton(this);
    ui->clocksTableWidget->setCellWidget(old_r_count, 0, radiobutton_item);
    QLabel *label_item = new QLabel(this);
    label_item->setAlignment(Qt::AlignCenter);
    label_item->setPixmap(QPixmap(path_to_image).scaled(90, 68, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    ui->clocksTableWidget->setCellWidget(old_r_count, 1, label_item);
    QTableWidgetItem *name_item = new QTableWidgetItem;
    name_item->setText(name_for_item);
    name_item->setData(32, cur_path);
    name_item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    ui->clocksTableWidget->setItem(old_r_count,2,name_item);
    }@

    1. How to make the radiobutton align to center? I mean if i make it like this
      @QWidget* wdg = new QWidget;
      QRadioButton radiobutton_item = new QRadioButton(this);
      QHBoxLayout
      layout = new QHBoxLayout(wdg);
      layout->addWidget(radiobutton_item);
      layout->setAlignment( Qt::AlignCenter );
      layout->setMargin(0);
      wdg->setLayout(layout);@

    if you click on a radiobutton of another tablewidget row, it won't uncheck the previous one because they are not on radiobuttongroup anymore.

    1. Clicking on the radiobuttons of a cellwidget hides the selection of the row. It is selected but not viewable to user (for example in Windows it is gray but should be blue...)
      before:
      !http://i.imgur.com/UMzsrhX.png(1)!

    after clicking:
    !http://i.imgur.com/gUDl22A.png(2)!

    selectRow on signal cellclicked doesn't work.

    1. How can i hide this gray line when i select an item of the tablewidget?
      !http://i.imgur.com/MoTrY6h.png(gray line)!

    4.How to access a cellwidget of a tablewidget? I tried:
    @void MainWindow::on_clocksTableWidget_clicked(const QModelIndex &index)
    {
    qDebug() << QRadioButton(ui->clocksTableWidget->cellWidget(index.row(), 0 )).isChecked();
    QRadioButton(ui->clocksTableWidget->cellWidget(index.row(), 0 )).setChecked(true);
    }@

    but ain't working. debug outputs always false and set checked does nothing.
    the index.row is the row that got clicked, and column 0 is the one i add the radiobuttons :(


  • Moderators

    1. you gave the answer to your question already. Just set the widget with the layout as cell widget instead the radio button

    2. try setting the focus policy of the radio button to Qt::NoFocus

    3. trick the view, so that it thinks it has no current item:
      @
      class MyItemDelegate : public QStyledItemDelegate
      {
      public:
      MyItemDelegate(QObject *parent = NULL) : QStyledItemDelegate(parent)
      {
      }

         void paint(QPainter *painter, const QStyleOptionViewItem &option,  const QModelIndex &index) const
         {
                 QStyleOptionViewItem opt = option;
                 opt.state &= ~QStyle::State_HasFocus;
                 QStyledItemDelegate::paint(painter, opt, index);
         }
      

    };
    @

    1. you are doing it wrong. try this instead:
      @
      void MainWindow::on_clocksTableWidget_clicked(const QModelIndex &index)
      {
      QRadioButton* radioButton = qobject_cast<QRadioButton*>(ui->clocksTableWidget->cellWidget(index.row(), 0 ) );
      if( radioButton )
      {
      qDebug() << radioButton.isChecked() << radioButton.setChecked(true);
      }
      }
      @


  • (1) Down from the answer i also wrote

    if i make it like this <codehere> if you click on a radiobutton of another tablewidget row, it won’t uncheck the previous one because they are not on radiobuttongroup anymore.

    (2) thank you

    (3) which disables the text and the focus completely?
    !http://i.imgur.com/ZTUSZWF.png(1)!

    I am not really experienced with delegates, maybe i am doing something wrong.
    @ClockTableWidgetDelegate delegate;
    ui->clocksTableWidget->setItemDelegate(&delegate);@

    (4) Tried this one before, if(radioButton) is always false.


  • Moderators

    1. i don't see any radio button group in your code?!

    2. you create the delegate on the stack. Thus it gets deleted immediatly. Create a pointer variable and pass it.

    3. if you set a QRadioButton* with setCellWidget(), cellWidget() should also return a QRadioButton*, thus the cast should definitly work



  • (1) setting a label as cellwidget (that contains an aligned to center radiobutton) breaks question 4 because i can't see checkstate of cellwidget.

    Also if radiobuttons are not in a label, they are automatically in a buttongroup for the tablewidget

    (3) hmm, yes forgot about it. I put a
    @ClockTableWidgetDelegate delegate;@ before the constructor of the mainwindow.cpp file and it worked. I am confused on were should i put a pointer at the .h file (i guess using pointer for the delegates is better coding?)

    if i put it on mainwindow class
    .h file: ClockTableWidgetDelegate does not name a type
    If i put it at ClockTableWidgetDelegate class (the QStyledItemDelegate)
    .cpp file: delegate was not declared on this scope

    (4) It works if i don't use a label which has the radiobutton inside.. Which i guess is reasonable. So what do we do here? I think one poor solution would be to create an empty column with width 5 because header of the tablewidget is not visable to user


  • Moderators

    just create a custom widget, which gives you access to the QRadioButton.
    Or do this workaround:
    @
    void MainWindow::on_clocksTableWidget_clicked(const QModelIndex &index)
    {
    QWidget* w = ui->clocksTableWidget->cellWidget(index.row(), 0 );
    if( w )
    {
    QRadioButton* radioButton = w->findChild<QPushButton*>();
    if( radioButton )
    {
    qDebug() << radioButton.isChecked() << radioButton.setChecked(true);
    }
    }
    }
    @



  • That's a nice trick you got there :)

    Do you know were should i put the pointer for the delegate?
    as said before:
    if i put it on mainwindow class
    .h file: ClockTableWidgetDelegate does not name a type
    If i put it at ClockTableWidgetDelegate class (the QStyledItemDelegate)
    .cpp file: delegate was not declared on this scope

    Besides that this is my final code (Seems good to me, everything is working perfectly):

    @clocksRadioButtonsGroup = new QButtonGroup(this);
    for(int i=0; i<2; i++)
    {
    int old_r_count=ui->clocksTableWidget->rowCount();
    ui->clocksTableWidget->insertRow(ui->clocksTableWidget->rowCount());
    QWidget* wdg = new QWidget;
    QRadioButton radiobutton_item = new QRadioButton(this);
    clocksRadioButtonsGroup->addButton(radiobutton_item);
    connect(radiobutton_item, SIGNAL(clicked()), this, SLOT(clockRadioButton_check_state_changed()));
    radiobutton_item->setFocusPolicy(Qt::NoFocus);
    QHBoxLayout
    layout = new QHBoxLayout(wdg);
    layout->addWidget(radiobutton_item);
    layout->setAlignment( Qt::AlignCenter );
    layout->setMargin(0);
    wdg->setLayout(layout);
    ui->clocksTableWidget->setCellWidget(old_r_count, 0, wdg);
    QLabel *label_item = new QLabel(this);
    label_item->setAlignment(Qt::AlignCenter);
    label_item->setPixmap(QPixmap(path_to_image).scaled(90, 68, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    ui->clocksTableWidget->setCellWidget(old_r_count, 1, label_item);
    QTableWidgetItem *name_item = new QTableWidgetItem;
    name_item->setText(name_for_item);
    name_item->setData(32, cur_path);
    name_item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    ui->clocksTableWidget->setItem(old_r_count,2,name_item);
    }
    }

    void MainWindow::clockRadioButton_check_state_changed()
    {
    int row_count=ui->clocksTableWidget->rowCount();
    for(int i=0; i<row_count; i++)
    {
    QWidget* w = ui->clocksTableWidget->cellWidget(i, 0 );
    if( w )
    {
    QRadioButton* radioButton = w->findChild<QRadioButton*>();
    if( radioButton && radioButton->isChecked())
    {
    ui->clocksTableWidget->selectRow(i);
    break;
    }
    }
    }
    }

    void MainWindow::on_clocksTableWidget_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
    {
    if(currentRow==previousRow)
    return;

    QWidget* w = ui->clocksTableWidget->cellWidget(currentRow, 0 );
    if( w )
    {
        QRadioButton* radioButton = w->findChild<QRadioButton*>();
        if( radioButton )
        {
            radioButton->setChecked(true);
        }
    }
    

    }
    @


  • Moderators

    just do declare it where you set it. When you also set a parent it gets deleted automatically by Qt:
    @
    ClockTableWidgetDelegate* delegate = new ClockTableWidgetDelegate(ui->clocksTableWidget);
    ui->clocksTableWidget->setItemDelegate(delegate);
    @



  • I appreciate your help!
    Still learning :)


Log in to reply
 

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