Accessing specific controls from widgets
-
// [Add Checkbox] { QWidget* pChkWidget = new QWidget(); usrCheckBox* pChkItem = new usrCheckBox(pChkWidget); pChkItem->SetCheckID(nRowIndex); pChkItem->setFixedSize(size.height() * 0.055, size.height() * 0.055); this->connect(pChkItem, &usrCheckBox::clicked, this, &wMethodInfo::BtnChecked); QHBoxLayout* layout = new QHBoxLayout(pChkWidget); layout->addWidget(pChkItem); layout->setAlignment(Qt::AlignCenter); layout->setContentsMargins(QMargins(0, 0, 0, 0)); pChkWidget->setLayout(layout); this->ui.fileTableWidget->setCellWidget(nRowIndex, 0, pChkWidget); }
I have such code.
The widget is put into the cell by calling the setCellWidget function.
We got the widget by calling the CellWidget(row, col) function.
I want to get the state of a checkbox inside a widget.
Is there a way to access the control by accessing it as a widget? -
QWidget* pChkWidget = new QWidget();
If I do setCellWidget after declaring it as usrCheckBox without declaring it as qwidget, it seems to come out as I want.
But I have to use Layout. please see my code
@IknowQT
Please be logical about this, and examine your own code. So far as I can see, glancing, I think you have a hierarchy like:setCellWidget(nRowIndex, 0, pChkWidget); QWidget* pChkWidget = new QWidget() QHBoxLayout* layout = new QHBoxLayout(pChkWidget); usrCheckBox* pChkItem = new usrCheckBox(pChkWidget);
So clearly picking up the
cellWidget()
is not going to return yourusrCheckBox
, it is going to return yourpChkWidget
, right?So from there to get to the
usrCheckBox
you must start from that widget and then use one of:- Find its child
QHBoxLayout
, find the childQWidget
which is on that, and cast that tousrCheckBox*
; or - Call
QObject::findChild<usrCheckBox*>()
on it to let it find the (first) descendant which is of typeusrCheckBox*
.
- Find its child
-
// [Add Checkbox] { QWidget* pChkWidget = new QWidget(); usrCheckBox* pChkItem = new usrCheckBox(pChkWidget); pChkItem->SetCheckID(nRowIndex); pChkItem->setFixedSize(size.height() * 0.055, size.height() * 0.055); this->connect(pChkItem, &usrCheckBox::clicked, this, &wMethodInfo::BtnChecked); QHBoxLayout* layout = new QHBoxLayout(pChkWidget); layout->addWidget(pChkItem); layout->setAlignment(Qt::AlignCenter); layout->setContentsMargins(QMargins(0, 0, 0, 0)); pChkWidget->setLayout(layout); this->ui.fileTableWidget->setCellWidget(nRowIndex, 0, pChkWidget); }
I have such code.
The widget is put into the cell by calling the setCellWidget function.
We got the widget by calling the CellWidget(row, col) function.
I want to get the state of a checkbox inside a widget.
Is there a way to access the control by accessing it as a widget?@IknowQT said in Accessing specific controls from widgets:
Is there a way to access the control by accessing it as a widget?
Yes. What exactly is the problem?
I think you need to cast from QWidget* to what ever the widget actually is (https://doc.qt.io/qt-6/qobject.html#qobject_cast-1). -
@IknowQT said in Accessing specific controls from widgets:
젯으로 액세스하여 컨트롤에 액세스하는 방법이 있습니까?
Inside the widget of pChkWidget is the pChkItem control.
The return value of this->ui.fileTableWidget->cellWidget(row, col) is Widget.
You want to access the PchkItem inside the pChkWidget. -
@IknowQT said in Accessing specific controls from widgets:
젯으로 액세스하여 컨트롤에 액세스하는 방법이 있습니까?
Inside the widget of pChkWidget is the pChkItem control.
The return value of this->ui.fileTableWidget->cellWidget(row, col) is Widget.
You want to access the PchkItem inside the pChkWidget.@IknowQT said in Accessing specific controls from widgets:
You want to access the PchkItem inside the pChkWidget.
I already wrote you that you need to cast QWidget* to the type you need.
Did you actually read my response? -
@IknowQT said in Accessing specific controls from widgets:
You want to access the PchkItem inside the pChkWidget.
I already wrote you that you need to cast QWidget* to the type you need.
Did you actually read my response?for (int i = 0; i < this->ui.fileTableWidget->rowCount(); i++)
{
usrCheckBox* pChkItem = static_cast<usrCheckBox*>(this->ui.fileTableWidget->cellWidget(i, 0));
qDebug() << "GetSelectedItems()"<< pChkItem->GetCheckID();
}I tested it like this.
I didn't get the results I wanted.Casting the widget doesn't seem to work.
-
for (int i = 0; i < this->ui.fileTableWidget->rowCount(); i++)
{
usrCheckBox* pChkItem = static_cast<usrCheckBox*>(this->ui.fileTableWidget->cellWidget(i, 0));
qDebug() << "GetSelectedItems()"<< pChkItem->GetCheckID();
}I tested it like this.
I didn't get the results I wanted.Casting the widget doesn't seem to work.
@IknowQT You should really read more carefully! I suggested to use https://doc.qt.io/qt-6/qobject.html#qobject_cast-1 for casting, not static_cast.
-
@IknowQT You should really read more carefully! I suggested to use https://doc.qt.io/qt-6/qobject.html#qobject_cast-1 for casting, not static_cast.
-
usrCheckBox* pChkItem = qobject_cast<usrCheckBox*>(this->ui.fileTableWidget->cellWidget(i, 0));
I tested it, but it comes out as Null after casting.
I don't think there is any way to access this@IknowQT said in Accessing specific controls from widgets:
I don't think there is any way to access this
Of course there is, but as @ChrisW67 wrote: what this->ui.fileTableWidget->cellWidget(i, 0) returns is apparently not an usrCheckBox...
-
@IknowQT said in Accessing specific controls from widgets:
I don't think there is any way to access this
Of course there is, but as @ChrisW67 wrote: what this->ui.fileTableWidget->cellWidget(i, 0) returns is apparently not an usrCheckBox...
-
QWidget* pChkWidget = new QWidget();
If I do setCellWidget after declaring it as usrCheckBox without declaring it as qwidget, it seems to come out as I want.
But I have to use Layout. please see my code
@IknowQT
Please be logical about this, and examine your own code. So far as I can see, glancing, I think you have a hierarchy like:setCellWidget(nRowIndex, 0, pChkWidget); QWidget* pChkWidget = new QWidget() QHBoxLayout* layout = new QHBoxLayout(pChkWidget); usrCheckBox* pChkItem = new usrCheckBox(pChkWidget);
So clearly picking up the
cellWidget()
is not going to return yourusrCheckBox
, it is going to return yourpChkWidget
, right?So from there to get to the
usrCheckBox
you must start from that widget and then use one of:- Find its child
QHBoxLayout
, find the childQWidget
which is on that, and cast that tousrCheckBox*
; or - Call
QObject::findChild<usrCheckBox*>()
on it to let it find the (first) descendant which is of typeusrCheckBox*
.
- Find its child
-
QWidget* pChkWidget = new QWidget();
If I do setCellWidget after declaring it as usrCheckBox without declaring it as qwidget, it seems to come out as I want.
But I have to use Layout. please see my code
@IknowQT So, in fact you're setting pChkWidget (which is a QWidget) as cell widget, right? Then why do you expect this->ui.fileTableWidget->cellWidget(i, 0) to return usrCheckBox?!
As @JonB wrote usrCheckBox is a child of pChkWidget. So get pChkWidget first (calling this->ui.fileTableWidget->cellWidget(i, 0)) and then its child usrCheckBox. -
Another approach would be to define your own widget class that exposes the state of the check box it contains.
class FancyCellWidget: pubic QWidget { Q_OBJECT public: explicit FancyCellWidget(QWidget *p = nullptr); Qt::CheckState checkState() const; // just return this from the internal QCheckbox void setCheckState(Qt::CheckState state); // pass through to the internal QCheckbox ... signals: void clicked(); // connect the internal check box clicked() signal to this one in the constructor };
and then use that to create your cell widget.
-
@IknowQT said in Accessing specific controls from widgets:
젯으로 액세스하여 컨트롤에 액세스하는 방법이 있습니까?
Inside the widget of pChkWidget is the pChkItem control.
The return value of this->ui.fileTableWidget->cellWidget(row, col) is Widget.
You want to access the PchkItem inside the pChkWidget.@jsulm said in Accessing specific controls from widgets:
Yes. What exactly is the problem?
I think you need to cast from QWidget* to what ever the widget actually is (https://doc.qt.io/qt-6/qobject.html#qobject_cast-1).@IknowQT said in Accessing specific controls from widgets:
Inside the widget of pChkWidget is the pChkItem control.
The return value of this->ui.fileTableWidget->cellWidget(row, col) is Widget.
You want to access the PchkItem inside the pChkWidget.You recommended objectcast.
I said clearly. I have a Checkbox control inside a Widget, so if I cast an object, does it become a checkbox?I showed the test results. Returning as null is a natural result.
I think you suggested just object cast without looking closely at the code you asked in the first place.@JonB
I was able to access that control using findChild, which Jonby recommended. -
@jsulm said in Accessing specific controls from widgets:
Yes. What exactly is the problem?
I think you need to cast from QWidget* to what ever the widget actually is (https://doc.qt.io/qt-6/qobject.html#qobject_cast-1).@IknowQT said in Accessing specific controls from widgets:
Inside the widget of pChkWidget is the pChkItem control.
The return value of this->ui.fileTableWidget->cellWidget(row, col) is Widget.
You want to access the PchkItem inside the pChkWidget.You recommended objectcast.
I said clearly. I have a Checkbox control inside a Widget, so if I cast an object, does it become a checkbox?I showed the test results. Returning as null is a natural result.
I think you suggested just object cast without looking closely at the code you asked in the first place.@JonB
I was able to access that control using findChild, which Jonby recommended.@IknowQT said in Accessing specific controls from widgets:
so if I cast an object, does it become a checkbox
Casting does not magically change the type of an object. That's why qobject_cast will return nullptr if the object you're trying to cast is not of the type you want to cast to. Casting only works if the object is really an object of the target type.