Qt How to check state checkbox in QtableWidget - qt

Recently I found the way that checkbox places in the middle of QtableWidget item.
However, I do not know how to check state whether or not button is clicked.
Could you tell me how to check button state?
here is what Ive found code:
QWidget *pWidget = new QWidget();
QCheckBox *pCheckBox = new QCheckBox();
QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
pCheckBox->setCheckState(Qt::Checked);
pLayout->addWidget(pCheckBox);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0,0,0,0);
pWidget->setLayout(pLayout);
ui->tableWidget2->setCellWidget(2,2, pWidget);

Although this is very late you can solve it like this:
auto field = ui->tableWidget2->cellWidget(2, 2, pWidget);
std::cout << qobject_cast<QCheckBox*>(field)->isChecked() << std::endl;
This works for other types as well (QComboBox etc.). Although it would probably be better to just use the checkbox functionality that QTableWidgetItem already has.
This example might not work if you are using a tristate checkbox in which case you should call: checkState() and compare it to Qt::CheckState. If qobject_cast<T> does not work out you can use a reinterpret_cast<T>.

I assume you created your checkboxes in the QWidgetTable like this:
int row...;int column...;
...
QTableWidgetItem *checkBoxItem = new QTableWidgetItem();
checkBoxItem->setCheckState(Qt::Unchecked);
ui->Table->setItem(row, column, checkBoxItem);
You can check the status of the item that corresponds to your widget in another function like this:
void MainWindow::on_Table_cellClicked(int row, int column)
{
QTableWidgetItem *checkBoxState = ui->Table->item(row, column);
if(ui->Table->item(row,column)->checkState())
{
checkBoxState->setCheckState(Qt::Unchecked);
ui->Table->setItem(row, column, checkBoxState);
}
else
{
checkBoxState->setCheckState(Qt::Checked);
ui->Table->setItem(row, column, checkBoxState);
}
}

Related

How to set data source for a list view to contain custom data ? (and associate with QTableView)

I am trying to get listview and tableview working together.
The listview must be used for display, the tableview must be used for editing data. The tableview is created on demand in a popup widget (and it may never be needed).
I populate the listview, on start, from a text file - each row a line, with 2 entries separated by a tab. Easy.
The tableview will have to edit 2 columns separately... also, on listview click, I must be able to retrieve the first part of the split...
I have created a model subclass of QStringListModel.
QListView *m_myView = new QListView();
StringList *m_myList = new StringList();
QTextStream in(&myFile);
while (!in.atEnd())
{
QString temp = in.readLine();
if(!temp.isEmpty())
m_myList->append(temp);
}
myFile.close();
m_myView->setModel(m_myList);
where
class StringList : public QStringListModel
{
public:
void append (const QString& string)
{
insertRows(rowCount(), 1);
QModelIndex m = index(rowCount() - 1);
setData(m, string, Qt::EditRole);
QStringList splist = string.split('\t');
setData(m, splist.at(0), Qt::ToolTipRole);
if(splist.size() > 1)
setData(m, splist.at(1), Qt::StatusTipRole);
else
setData(m, "", Qt::StatusTipRole);
qDebug() << data(m, Qt::EditRole).toString()
<< data(m, Qt::ToolTipRole).toString()
<< data(m, Qt::StatusTipRole).toString();
}
};
The EditRole displays correctly, the others display empty strings.
It seems that QStringListModel is incapable of storing anything except EditRole.
So I am left with 2 options - either do the split on each selection, and also when creating the table view, or - what I would like to try but don't know how - create a QStandardItemModel that can act as data sources for both the listview and the tableview, and can easily retrieve the partial data I require on click.
I thought I can simply use it to set the data on listview (like they do here).
QListView *m_myView = new QListView();
QStandardItemModel *m_myList = new QStandardItemModel();
QTextStream in(&myFile);
int r = 0;
while (!in.atEnd())
{
QString temp = in.readLine();
if(!temp.isEmpty())
{
QStringList splist = temp.split('\t'); // assume I know there will be 2 strings always
QStandardItem *item = new QStandardItem(splist.at(0));
m_myList->setItem(r, 0, item);
QStandardItem *item1 = new QStandardItem(splist.at(1));
m_myList->setItem(r, 1, item1);
++r;
}
}
myFile.close();
m_myView->setModel(m_myList);
connect(m_myListView, SIGNAL(clicked(QModelIndex)),
this, SLOT(listChangedSlot(QModelIndex)));
But this will only set the first string in the listview, I really need both, and I still don't know how to retrieve the data
void listChangedSlot(QModelIndex index)
{
qDebug() << m_myList->item(index.row(), 0)->data().toString()
<< m_myList->item(index.row(), 1)->data().toString();
} // shows empty strings
Or...
In the loading section, try:
if(!temp.isEmpty())
{
QStringList splist = temp.split('\t');
splist.append(QString()); // so I don't need to test
QStandardItem *item = new QStandardItem(temp);
m_myList->setItem(r, 0, item);
QModelIndex idx = m_myList->index(r, 0);
QMap<int, QVariant> roles;
roles.insert(Qt::UserRole + 1, QVariant(splist.at(0)));
roles.insert(Qt::UserRole + 2, QVariant(splist.at(1)));
roles.insert(Qt::DisplayRole, QVariant(temp));
m_myList->setItemData(idx, roles);
++r;
}
This works - displays fine and gets the correct content on click - but seems to be unusable for the tableview.
(And seems I am doing twice the work, with setItem and setItemData - so technically storing the content 3 times).
How can I get my listview have a datasource with 2 string items, show both, be able to set it on a tableview, and be able to retrieve the first item on click ?
I figured out a way to share the data source between the listview and tableview:
I create 3 columns - column 0 with the combined components, and columns 1 and 2 with the separated components.
The listview will display only column 0. For tableview, I will hide column 0.
The data I require for processing will be stored in columns 1 and 2. user edit of the data in tableview will require, upon accepting changes, the edit of corresponding column 0.

Making a dynamic QMenu in QT

I have a QMenu, where I want to add QActions. The problem is, everytime the table is clicked, the existing menu adds onto itself. How do I stop it from doing that?
So when I click on the table, it should be new menus everytime.
void MainWindow::onTableClicked(const QModelIndex &index){
QAction *action;
// action->setAutoRepeat(false);
if (index.isValid()) {
QString cellText = index.data().toString();
QString indexRow = QString::number(index.row());
QString indexCol = QString::number(index.column());
ui->textBrowser->setText(indexRow + "\n"+ indexCol);
QSet<int> possiblevalues = findPossibleValues(index.row(),index.column());
for(int n : possiblevalues){
listofPossibleValues.push_back(QString::number(n));
}
for(auto s :listofPossibleValues){
action = myMenu.addAction("Set Value "+s);
}
}
}
The reason why it looked like myMenu.clear wasnt working was because i did not remove listofPossibleValues after I added the menuaction. I was able to fix it by adding
//At the beginning
myMenu.clear();
//At the end
listofPossibleValues.clear();

How to work with signals from QTableWidget cell with cellWidget set

I've got a QTableWidget with some columns inside.
Due to my needs I set QComboBox inside some columns and fill them with necessary data.
void settingsDialog::onAddFieldButtonClicked()
{
fieldsTable->setRowCount(++rowCount);
combo = new QComboBox();
combo->addItem(QString("Choose from list..."));
foreach( int height, heightsAvailable)
combo->addItem(QString("%1").arg(height));
fieldsTable->setCellWidget(rowCount-1, 3, combo);
// etc for other columns ...
}
The question is how to catch signals from these combo boxes if they were changed?
I want to know row and col of changed widget (combo box) and the value which was set.
I've tried all available signals which are mentioned in Qt docs for QTableWidget, but they work only if cell doesn't have widget inside it.
Is there a simple and Qt-way to get what I need?
Instead of handling a signal from the table, you can handle currentIndexChanged signal from combo box itself.
QComboBox* combo = new QComboBox();
combo->addItem(QString("Choose from list..."));
combo->addItem(QString("first item"));
combo->setProperty("row", ui->tableWidget->rowCount() - 1);
combo->setProperty("column", 0);
connect(combo, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(OnComboIndexChanged(const QString&)));
ui->tableWidget->setCellWidget(ui->tableWidget->rowCount() - 1, 0, combo);
And in the slot, you can use sender() to identify the combo box which emitted the signal.
void MainWindow::OnComboIndexChanged(const QString& text)
{
QComboBox* combo = qobject_cast<QComboBox*>(sender());
if (combo)
{
qDebug() << "row: " << combo->property("row").toInt();
qDebug() << "column: " << combo->property("column").toInt();
}
}

How to get the text of a combobox in a tablewidget?

I’m new to Qt, I need help with getting the value of a combobox in a table widget.
I use “setCellWidget” to add a combobox(in my case, its name is “settingA”) to a table widget (the name is “tableWidget_4”):
QComboBox* settingA = new QComboBox();
settingA->addItem("100");
settingA->addItem("200");
ui->tableWidget_4->setColumnCount(1);
ui->tableWidget_4->setRowCount(3);
ui->tableWidget_4->setCellWidget ( 0, 0, settingA );
What I want to do here is:
When a button (its name is “ApplyComboButton” in my case) is clicked, I want the value of the combobox(settingA) can be saved into a QStringList(InputComboData) , and this is how I try to do this:
void MainWindow::on_ApplyComboButton_clicked()
{
QStringList InputComboData;
InputComboData << ui->tableWidget_4->item(0,0)->text();
}
And it fails.
How can I get the value of my combobox?
You can use the QTableWidget::cellWidget ( int row, int column ) function to get your QComboBox widget. Use qobject_cast to cast it to QComboBox, and use the currentText() function to get the text.
QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget_4->cellWidget(0,0));
InputComboData << myCB->currentText();
Use QTableWidget's cellWidget to get a QWidget* to the widget you set as cellWidget (don't forget to use qobject_cast or dynamic_cast to cast that pointer to a QCombobox*)

Qt - how to access QComboBox data inserted in QTreeWidget

During runtime I have inserted QCombobox in my QTreeWidget like this:
//global defines
#define COLUMN_1 (0)
#define COLUMN_2 (1)
//Init QComboBox to QTreeWidget - works fine.
QTreeWidgetItem *item = new QTreeWidgetItem(_myTreeWidget);
item->setText(COLUMN_1,"testing");
QComboBox *box = new QComboBox();
box->addItem("select1");
box->addItem("select2");
box->addItem("select3");
_myTreeWidget->setItemWidget(item, 1, box);
The above code works, but I also want to read the data text in these columns. Eg. get the strings "testing" & "select2" from code above. The problem is that I can't figure out how to read the "QComboBox::currentText()" in the comboboxes.
I have tried:
QTreeWidgetItemIterator it(_myTreeWidget);
while(*it)
{
QTreeWidgetItem *item = *it;
QVariant first = item->text(COLUMN_1);
QString firstStr = loggerName.toString(); //this works
QComboBox *box = (QComboBox*)item->data(COLUMN_2, 0);
QString boxValStr = box->text().toString(); //this doesn't works, always empty string
//... more code to handle strings...
it++;
}
Feels like the "item->data(COLUMN_2, 0)" is wrong way to go cause it returns a QVariant.
Solution on this problem?
QComboBox *box = (QComboBox*)item->data(COLUMN_2, 0);
When I read this code, I went into panic mode. Look at the signature:
QVariant QTreeWidgetItem::data ( int column, int role ) const
As you used setItemWidget, you should probably use
QWidget * QTreeWidget::itemWidget ( QTreeWidgetItem * item, int column ) const
ps: If you want to cast, use C++ casts. Much better, use qobject_cast<SubtypeofQObjectPtr> for QObject. It returns null when the cast is invalid.
Indeed, I mean retrieve the combobox using a call similar to :
QComboBox* box = qobject_cast<QComboBox*>(treeWidget->itemWidget(item, column));
Solved it thanks to the help from #Umnyobe and #Zaiborg above. Here is a total working example:
Init QTreeWidget with text in column1 and QComboBox in column2:
//global defines
#define COLUMN_1 (0)
#define COLUMN_2 (1)
QTreeWidgetItem *item = new QTreeWidgetItem(_myTreeWidgetPtr);//item to put in tree
item->setText(COLUMN_1,"animal"); //item for column 1 in the tree.
QComboBox *box = new QComboBox();
box->addItem("mouse"); //adds selections for comboboxes
box->addItem("cat");
box->addItem("dog");
_myTreeWidgetPtr->setItemWidget(item, COLUMN_2, box); //insert items in tree.
Read values from tree:
QTreeWidgetItemIterator it(_myTreeWidgetPtr);
while(*it)
{
QTreeWidgetItem *item = *it;
//Init pointer to current combobox
QComboBox* box = qobject_cast<QComboBox*>(_myTreeWidgetPtr->itemWidget(item, COLUMN_2));
//Get data from QTreeWidget
QString col1Str = item->text(COLUMN_LOGGER);
QString col2Str = box->currentText();
it++;
}
hope it can help someone :)
use the QSignalMapper class to collect the different boxes in the treewidget.
then connect the QSignalMapper::mapped() signal to some slot and use the combobox
edit:
QSignalMapper* mapper = new QSignalMapper(this);
QComboBox *box = new QComboBox();
connect( box, SLOT(/*whatever*/), mapper, SLOT( map() ) );
mapper->setMapping( box );
myTreeWidget->setItemWidget(item, 1, comboBox);
For anyone who is looking for a Python solution,
(PySide / PyQt QComboBox in QTreeWidget), here is it:
item = QTreeWidgetItem(self.treeWidgetAnimals)
item.setText(0, "animal")
combo_box = QComboBox()
combo_box.addItem('mouse')
combo_box.addItem('cat')
combo_box.addItem('dog')
self.treeWidgetAnimals.setItemWidget(item, 1, combo_box)
I was looking for hours but no other forum like pass reference "parent" like a "delegation":
        item = QTreeWidgetItem (self.myTreeWidgetItemObject)
if you do not pass the parent, error ir not returned but the ComboBox not appears in display TreeWidget.

Resources