如何将窗口窗体保存在 QTableWidget 中,因为它是矩阵?
我正在尝试将 Window form
保存在 QTableWidget
表中。
int rows = 0;
int columns = 0;
QTableWidget cellTable;
我所做的是,我首先设置行和列
cellTable.setRowCount(++rows);
cellTable.setRowCount(++columns);
每次增加行数时,我都会调用此代码
for(int i = 0; i < rows; i++)
cellTable.setCellWidget(rows-1, i, new DatabaseMeasurementType());
每次增加列数时,我都会调用此代码
for(int i = 0; i < rows; i++)
cellTable.setCellWidget(i, columns-1, new DatabaseMeasurementType());
如果我替换 new DatabaseMeasurementType( )
和 new QPushButton()
。但随后所有字段都将是按钮。目前,我只想在 setCellWidget
中存储一个窗口表单。是的,new DatabaseMeasurementType()
是一个小部件表单。
问题:
当我调用此代码(动态转换)
DatabaseMeasurementType *databaseMeasurementType = static_cast<DatabaseMeasurementType*>(cellTable.cellWidget(row, column));
databaseMeasurementType->show();
或此代码(静态转换)
DatabaseMeasurementType *databaseMeasurementType = dynamic_cast<DatabaseMeasurementType*>(cellTable.cellWidget(row, column));
databaseMeasurementType->show();
时,它会崩溃。为什么? 我的想法是拥有一个大型的 2D 矩阵,可以在其中存储 Windows 表单。然后我只需要调用 2D 矩阵的 x 和 y 参数即可接收 Windows 表单。
I'm trying to save a Window form
inside a QTableWidget
table.
int rows = 0;
int columns = 0;
QTableWidget cellTable;
What I'm doing is that I first set the rows and columns
cellTable.setRowCount(++rows);
cellTable.setRowCount(++columns);
For every time I increase the rows, I call this code
for(int i = 0; i < rows; i++)
cellTable.setCellWidget(rows-1, i, new DatabaseMeasurementType());
For every time I increase the columns, I call this code
for(int i = 0; i < rows; i++)
cellTable.setCellWidget(i, columns-1, new DatabaseMeasurementType());
It works if I replace the new DatabaseMeasurementType()
with new QPushButton()
. But then all the fields will be buttons. For the moment, I just want to store a window form inside the setCellWidget
. Yes, new DatabaseMeasurementType()
is a widget form.
Problem:
When I call this code (dynamic cast)
DatabaseMeasurementType *databaseMeasurementType = static_cast<DatabaseMeasurementType*>(cellTable.cellWidget(row, column));
databaseMeasurementType->show();
Or this code (static cast)
DatabaseMeasurementType *databaseMeasurementType = dynamic_cast<DatabaseMeasurementType*>(cellTable.cellWidget(row, column));
databaseMeasurementType->show();
Then it crash. Why?
My idea is to have a large 2D matrix where I can store windows forms. Then I only need to call x
and y
arguments for the 2D matrix to recieve the windows form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正确地从 QWidget 继承,则转换应该可以工作。如果失败,则强制转换返回
nullptr
。因此,检查是否失败会更安全:如果您在类中编写了
Q_OBJECT
宏,建议使用qobject_cast
。如果您想在表格单元格之外显示窗口,
setCellWidget
方法时,QTableWidget由于内存管理而更改小部件的父级。小部件显示在其父级内部。因此,您应该在获取它后更改它的父级并将其从表中删除。但是QTableWidget::removeCellWidget(int row, int column)
会删除该小部件并将其删除。setCellWidget(row, col, nullptr)
也会删除它。因此,删除小部件而不删除它的一种方法是将其包装到另一个小部件。QWidget* clone()
方法,并逐个克隆成员。Casting should work if you've inherited from QWidget properly. Casts return
nullptr
if failed. So, it's safier to check if it's failed or not:It's recommended to use
qobject_cast
if you've writtenQ_OBJECT
macro inside your class.If you want to show your window outside of the table's cell
setCellWidget
method, QTableWidget change widget's parent because of memory management. Widgets are shown inside their parents. So, you should change it's parent after getting it and remove it from the table. ButQTableWidget::removeCellWidget(int row, int column)
removes the widget and deletes it.setCellWidget(row, col, nullptr)
also deletes it. So one way to remove the widget without deleting it is wrapping it to another widget.QWidget* clone()
method, and cloning member-by-member.