The simple answer would seem to be:
button.clicked.connect(cellClick)
But then how would you know the row/column of the cell that was clicked? So the next iteration might be:
button = QPushButton() table.setCellWidget(row, column, button) button.clicked.connect( lambda * args, row = row, column = column: cellClick(row, column))
Which is okay if the table is static. But what if the rows and columns can be rearranged? The row/column cached in the button's clicked
handler could be invalidated in that case. So the next iteration might be:
index = QtCore.QPersistentModelIndex(table.model().index(row, column)) button.clicked.connect( lambda * args, index = index: cellClick(index.row(), index.column()))
But then how would you know the anycodings_signals-slots row/column of the cell that was clicked? anycodings_signals-slots So the next iteration might be:,So, in short, my question is - how can I anycodings_pyqt select (click) a QTableWidget cell when anycodings_pyqt there's a button inside it?,then, cellClick function does its job. The anycodings_pyqt thing is: some cells contain a button, like anycodings_pyqt so (coordinates 0,0 as an example):,The simple answer would seem to be:
I have a question regarding QTableWidget at anycodings_pyqt PyQt4. Say I have a QTableWidget, where I anycodings_pyqt want to have an event connected to a cell anycodings_pyqt click using:
table.cellClicked.connect(cellClick)
then, cellClick function does its job. The anycodings_pyqt thing is: some cells contain a button, like anycodings_pyqt so (coordinates 0,0 as an example):
button = QtGui.QPushButton() table.setCellWidget(0, 0, button)
The simple answer would seem to be:
button.clicked.connect(cellClick)
But then how would you know the anycodings_signals-slots row/column of the cell that was clicked? anycodings_signals-slots So the next iteration might be:
button = QPushButton() table.setCellWidget(row, column, button) button.clicked.connect( lambda * args, row = row, column = column: cellClick(row, column))
Which is okay if the table is static. anycodings_signals-slots But what if the rows and columns can be anycodings_signals-slots rearranged? The row/column cached in the anycodings_signals-slots button's clicked handler could be anycodings_signals-slots invalidated in that case. So the next anycodings_signals-slots iteration might be:
index = QtCore.QPersistentModelIndex(table.model().index(row, column)) button.clicked.connect( lambda * args, index = index: cellClick(index.row(), index.column()))
I have a QTableWidget and the rows contain a column with a QRadioButton. When I check the radio button in a row which is not selected, I'd like to select this row automatically. How can I achieve this?, panosk last edited by Hi, I have a QTableWidget and the rows contain a column with a QRadioButton. When I check the radio button in a row which is not selected, I'd like to select this row automatically. How can I achieve this? Thanks. Reply Quote 0 2 Replies Last reply ,@panosk said in Select whole row in QTableWidget when radio button is checked:, Select whole row in QTableWidget when radio button is checked
In the meantime, I found a solution. The problem and the confusion was that when a radio button (or checkbox) is checked, two toggled
signals are emitted -one for the radio button actually clicked and one for the previously checked radio button which changes to unchecked, as the buttons are set to autoExclusive
. So, I worked with the model indexes and got the result I wanted (note that in the end I opted for a QCheckBox
):
void ServiceDialog::setActiveModel()
{
for (auto row = 0; row < ui->myTable->rowCount(); row++) {
QCheckBox *box = qobject_cast<QCheckBox*>(ui->myTable->cellWidget(row,5));
if (box && box->isChecked()) {
ui->myTable->setCurrentIndex(ui->myTable->model()->index(row,0));
ui->myTable->selectRow(ui->myTable->currentIndex().row());
theStringIwant = ui->myTable->item(ui->myTable->currentIndex().row(),0)->text();
break;
}
}
...
}
@jsulm ,
Thanks for the prompt reply. That's my intention but I can't figure it out. Since the rows are created dynamically, I'm creating the radio buttons in a slot, after the user clicks a button. In that slot, I'm using this connection after creating the radio button
connect(myRadioButton, & QRadioButton::toggled, this, & MyDialog::myOtherSlot);
This is a slot where I create the items and connect the toggled
signal of each radio button to another slot.
void ServiceDialog::addModel() {
...
QRadioButton * rButton = new QRadioButton(ui - > myTable);
ui - > myTable - > setCellWidget(ui - > myTable - > rowCount() - 1, 5, rButton);
//if this is the first item in the table, we want it to be enabled.
if (ui - > myTable - > rowCount() == 1) {
ui - > myTable - > selectRow(0);
rButton - > setChecked(true);
}
//We need to know when a radio button is clicked and act on that row
connect(rButton, & QRadioButton::toggled, this, & ServiceDialog::setActiveModel);
}
void ServiceDialog::setActiveModel()
{
//Ugly, but it shows the confused state of the radio buttons
for (auto row=0; row < ui->myTable->rowCount(); row++) {
QRadioButton *button = qobject_cast<QRadioButton*>(ui->myTable->cellWidget(row,5));
if (button) {
if (button->isChecked()) {
// Surprise! Here, the previously checked button is actually checked!! but in the
// table it appears unchecked!!,
// so it's impossible to find out which radio button is actually checked
// and work on that row
}
}
}
The QTableWidget class provides an item-based table view with a default model. More...,Table widgets provide standard table display facilities for applications. The items in a QTableWidget are provided by QTableWidgetItem.,The table widget will use the item prototype clone function when it needs to create a new table item. For example when the user is editing in an empty cell. This is useful when you have a QTableWidgetItem subclass and want to make sure that QTableWidget creates instances of your subclass.,Removes all items in the view. This will also remove all selections and headers. If you don't want to remove the headers, use QTableWidget::clearContents(). The table dimensions stay the same.
tableWidget = new QTableWidget(12, 3, this);
tableWidget = new QTableWidget(this);
tableWidget - > setRowCount(10);
tableWidget - > setColumnCount(5);
QTableWidgetItem * newItem = new QTableWidgetItem(tr("%1").arg(
(row + 1) * (column + 1)));
tableWidget - > setItem(row, column, newItem);
QTableWidgetItem * cubesHeaderItem = new QTableWidgetItem(tr("Cubes"));
cubesHeaderItem - > setIcon(QIcon(QPixmap(":/Images/cubed.png")));
cubesHeaderItem - > setTextAlignment(Qt::AlignVCenter);
setCellWidget(row, column, new QLineEdit);
...
setCellWidget(row, column, new QTextEdit);
QTableWidget with a column of buttons. How do I get whichone is clicked?, self.table.setItem(rowPos,2,QTableWidgetItem(str(avdPath))), self.table.setItem(rowPos,4,QTableWidgetItem(str(filetype))), self.table.setItem(rowPos,3,QTableWidgetItem(str(filename)))
self.table.cellClicked.connect(self.cell_was_clicked)
self.table.cellClicked.connect(self.cell_was_clicked)
self.table.cellClicked.connect(self.cell_was_clicked)
rowPos = self.table.rowCount() self.table.insertRow(rowPos) self.table.setItem(rowPos, 0, QTableWidgetItem(str(status))) self.table.setItem(rowPos, 1, QTableWidgetItem(str(snapshotID))) self.table.setItem(rowPos, 2, QTableWidgetItem(str(avdPath))) self.table.setItem(rowPos, 3, QTableWidgetItem(str(filename))) self.table.setItem(rowPos, 4, QTableWidgetItem(str(filetype))) self.table.setCellWidget(rowPos, 5, showBtn)
rowPos = self.table.rowCount() self.table.insertRow(rowPos) self.table.setItem(rowPos, 0, QTableWidgetItem(str(status))) self.table.setItem(rowPos, 1, QTableWidgetItem(str(snapshotID))) self.table.setItem(rowPos, 2, QTableWidgetItem(str(avdPath))) self.table.setItem(rowPos, 3, QTableWidgetItem(str(filename))) self.table.setItem(rowPos, 4, QTableWidgetItem(str(filetype))) self.table.setCellWidget(rowPos, 5, showBtn)
rowPos = self.table.rowCount() self.table.insertRow(rowPos) self.table.setItem(rowPos, 0, QTableWidgetItem(str(status))) self.table.setItem(rowPos, 1, QTableWidgetItem(str(snapshotID))) self.table.setItem(rowPos, 2, QTableWidgetItem(str(avdPath))) self.table.setItem(rowPos, 3, QTableWidgetItem(str(filename))) self.table.setItem(rowPos, 4, QTableWidgetItem(str(filetype))) self.table.setCellWidget(rowPos, 5, showBtn)
This method is also a Qt slot with the C++ signature void insertColumn(int).,This method is also a Qt slot with the C++ signature void insertRow(int).,This method is also a Qt slot with the C++ signature void removeColumn(int).,This method is also a Qt slot with the C++ signature void removeRow(int).
Table widgets can be constructed with the required numbers of rows and columns:
tableWidget = new QTableWidget(12, 3, this);
Alternatively, tables can be constructed without a given size and resized later:
tableWidget = new QTableWidget(this);
tableWidget - > setRowCount(10);
tableWidget - > setColumnCount(5);
Items are created ouside the table (with no parent widget) and inserted into the table with setItem():
QTableWidgetItem * newItem = new QTableWidgetItem(tr("%1").arg(
(row + 1) * (column + 1)));
tableWidget - > setItem(row, column, newItem);
If cell widget A is replaced with cell widget B, cell widget A will be deleted. For example, in the code snippet below, the QLineEdit object will be deleted.
setCellWidget(index, new QLineEdit);
...
setCellWidget(index, new QTextEdit);