如何将窗口窗体保存在 QTableWidget 中,因为它是矩阵?

发布于 2025-01-09 13:01:13 字数 1328 浏览 1 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡看悲欢离合 2025-01-16 13:01:14

如果您正确地从 QWidget 继承,则转换应该可以工作。如果失败,则强制转换返回 nullptr。因此,检查是否失败会更安全:

if(databaseMeasurementType != nullptr){
    //some works with databaseMeasurementType
}

如果您在类中编写了 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:

if(databaseMeasurementType != nullptr){
    //some works with databaseMeasurementType
}

It's recommended to use qobject_cast if you've written Q_OBJECT macro inside your class.

If you want to show your window outside of the table's cell

  • Getting widget and removing it from the table. When you use 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. But QTableWidget::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.
  • Cloning a widget. You should do it manually. One traditional way is to implement custom QWidget* clone() method, and cloning member-by-member.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文