[英]QT how to check table widget type
I have added a checkbox to my QTableWidget like this:我在 QTableWidget 中添加了一个复选框,如下所示:
QWidget *checkBoxWidget = new QWidget();
QCheckBox *checkBox = new QCheckBox(); // We declare and initialize the checkbox
QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); // create a layer with reference to the widget
layoutCheckBox->addWidget(checkBox); // Set the checkbox in the layer
layoutCheckBox->setAlignment(Qt::AlignCenter); // Center the checkbox
layoutCheckBox->setContentsMargins(0,0,0,0); // Set the zero padding
checkBox->setChecked(true);
ui->my_table_view->setCellWidget(0,1, checkBoxWidget); //
The problem is when I read that table cell how I will know that the cell holds a checkbox
, a radio-button
or a text
.问题是当我阅读该表格单元格时,我如何知道该单元格包含一个
checkbox
、一个radio-button
或一个text
。
Currently, my getValuesAt
function can read text from the cell but I am not sure how to check the checkbox
's state目前,我的
getValuesAt
函数可以从单元格读取文本,但我不确定如何检查checkbox
的状态
getValuesAt(int row_number, int column_number)
{
if(!ui->my_table_view->item(row_number,column_number)){
return "";
}else{
return ui->my_table_view->item(row_number,column_number)->text();
}
}
I tried to read widget type like this but it returns 0我试图读取这样的小部件类型,但它返回 0
QTableWidgetItem::ItemType res = ui->my_table_view->item(row_number,column_number)->Type;
Please notice that, if you put a widget in a cell, the corresponding item will be null unless you don't explicitly add one:请注意,如果您在单元格中放置一个小部件,除非您没有明确添加,否则相应的项目将为空:
tableWidget->setCellWidget(0, 0, new QCheckBox());
qDebug() << tableWidget->item(0, 0); //yields 0x0
When inspecting a cell , you could first check if it has a widget and, if it doesn't, check if the underlying item is null or not:检查cell 时,您可以首先检查它是否有小部件,如果没有,请检查基础项目是否为空:
for(int column=0; column<columns; ++column)
{
QWidget * cellWidget = tableWidget->cellWidget(row, column);
if(cellWidget != nullptr)
{
QCheckBox * checkbox = dynamic_cast<QCheckBox*>(cellWidget);
if(checkbox != nullptr)
{
qDebug() << checkbox->isChecked();
}
else
{
//some other widget type ...
}
}
else
{
QTableWidgetItem * item = tableWidget->item(row, column);
if(item != nullptr)
{
qDebug() << item->text();
}
else
{
//this is an empty cell
}
}
}
You can access class name of any QObject derived class by您可以通过以下方式访问任何 QObject 派生类的类名
QString className(someObject->metaObject()->className());
For you example it would be like对于你的例子,它会像
QString className(ui->my_table_view->item(row_number,column_number)->metaObject()->className());
You can cast a pointer of a QWidget
instance to a concrete type pointer, eg.您可以将
QWidget
实例的指针转换为具体类型指针,例如。 QCheckBox*
and check the result to be not nullptr
. QCheckBox*
并检查结果是否为nullptr
。 If it is not nullptr
, it is an instance of that type.如果它不是
nullptr
,则它是该类型的实例。
QWidget *widget = new QCheckBox();
if (dynamic_cast<QCheckBox*>(widget) != widget) {
// widget is a QCheckBox
}
if (dynamic_cast<QRadioButton*>(widget) == nullptr) {
// widget is not a QRadioButton
}
You can use other casting options as well.您也可以使用其他投射选项。 Like
qobject_cast
or static_cast
.像
qobject_cast
或static_cast
。
This method is useful not only for Qt classes.此方法不仅对 Qt 类有用。 You can check whether the coming pointer (no matter where it comes from) is an instance of this or that class (or inherited class).
您可以检查即将到来的指针(无论它来自哪里)是否是这个或那个类(或继承类)的实例。 But in other cases, you might not have a
qobject_cast
option, because it is Qt
specific.但在其他情况下,您可能没有
qobject_cast
选项,因为它是特定于Qt
。 But others remain applicable.但其他人仍然适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.