简体   繁体   English

用新的QWidget替换QWidget

[英]Replace QWidget with a new QWidget

This questions to me reeks of maybe a lack of understanding of C++, as the possibilities I've considered for my problem all seem to make no sense on why this could be occuring. 这个问题让我觉得可能缺乏对C ++的理解,因为我对我的问题所考虑的可能性似乎都没有理由为什么会发生这种情况。 Feedback appreciated. 反馈意见。

I'm using the form designer to create a form class with a table in it. 我正在使用表单设计器来创建一个包含表格的表单类。 I'm trying to replace the table with another table generated in a helper class. 我正在尝试用辅助类中生成的另一个表替换该表。 I'm only doing this so I can (hopefully) maintain the nice grid layout I've designed, and through pointer manipulation, get the replacement I desire. 我只是这样做,所以我可以(希望)保持我设计的漂亮网格布局,并通过指针操作,获得我想要的替代品。 Here's some code snippets from the table form constructor and relevant calls : 以下是表格构造函数和相关调用的一些代码片段:

//tableData is defined in the header file as a QTableWidget*
tableData = this->findChild<QTableWidget *>("tableData");
....
setup();

void setup(){
     tableData = Utilities::createTable(this->file, tableDelim);
     //createTable returns QTableWidget*
     ... other assignments, and label text updates, which seem to all work
}

My understanding is that tableData is a pointer, and if printed, will give the address of the QTableWidget from the layout. 我的理解是tableData是一个指针,如果打印,将从布局中给出QTableWidget的地址。 So then if I create a QTableWidget* and then assign tableData to that, tableData should now point to the new widget. 那么如果我创建一个QTableWidget *然后将tableData分配给它,tableData现在应该指向新的小部件。 Instead, I see only a blank screen. 相反,我只看到一个空白的屏幕。

I tried checking what the tableData pointer is before I assign it to the new QTableWidget*, and after. 在将其分配给新的QTableWidget *之后,我尝试检查tableData指针是什么。 The second pointer shown is what is generated by createTable() : 显示的第二个指针是createTable()生成的:

QTableWidget(0x101272d40, name = "tableData") QTableWidget(0x10127b3b0, name = "test_sample2.nuc.stats") 
QTableWidget(0x10127b3b0, name = "test_sample2.nuc.stats") QTableWidget(0x10127b3b0, name = "test_sample2.nuc.stats") 

It seems the pointer is being reassigned, but the table drawn isn't the right one. 似乎指针被重新分配,但绘制的表格不正确。

What gives? 是什么赋予了?

You are right with your assumption. 你的假设是正确的。 You do set a variable to be a pointer to a object and next you set the variable to be a pointer to another object. 您将变量设置为指向对象的指针,然后将变量设置为指向另一个对象的指针。 You never change any objects, just your variable which is not used to display anything. 你永远不会改变任何对象,只改变你用来显示任何东西的变量。

You would need to do something like: 您需要执行以下操作:

//tableData is defined in the header file as a QTableWidget*
tableData = this->findChild<QTableWidget *>("tableData");

parentLayout = tableData->parent()->layout(); //Get the parent widget to add another table.
parentLayout->removeWidget(tableData);
delete tableData;
parentLayout->addWidget(createTable());

My understanding is that you want to design the table layout in designer but fill in the data from an external source. 我的理解是你想在设计器中设计表格布局,但是要从外部源填充数据。

I would suggest, to just use the QTableWidget that is created in setupUi() and modify Utilities::createTable() such that it becomes Utilities::populateTable(QTableWidget & table, <all the other parameters you need>) . 我建议,只需使用在setupUi()创建的QTableWidget并修改Utilities::createTable() ,使其成为Utilities::populateTable(QTableWidget & table, <all the other parameters you need>) (Or use QTableWidget * if you prefer - however I like putting the non-zero assertion responsibility on the caller...) (或者如果您愿意,可以使用QTableWidget * - 但是我喜欢将非零断言责任放在调用者身上......)

Apart from that, I agree with Sebastian Lange. 除此之外,我同意Sebastian Lange的观点。

You need to use pTheContainerOfTheOriginalTableWidget->addWidget(tableData); 你需要使用pTheContainerOfTheOriginalTableWidget->addWidget(tableData); See here: http://qt-project.org/forums/viewthread/16547 见这里: http//qt-project.org/forums/viewthread/16547

Be sure you remove the original tableWidget so you don't have two (I assume you don't want two). 一定要删除原来的tableWidget,这样你就没有两个(我假设你不想要两个)。

If I understand you correctly we have such situation. 如果我理解你,我们就有这样的情况。

call of setupUi (which generated by qt tootls), there there is something like this(pseudo code): 调用setupUi(由qt tootls生成),有这样的东西(伪代码):

oldTablePtr = new QTableWidget(parent); oldTablePtr = new QTableWidget(parent); someLayout->addWidget(oldTablePtr); someLayout-> addWidget(oldTablePtr);

So parent and layout hold value of oldTablePtr. 因此父级和布局保持oldTablePtr的值。

And if you set variable oldTablePtr nothing changed. 如果你设置变量oldTablePtr没有改变。 parent send QPaintEvent to oldTablePtr. parent将QPaintEvent发送到oldTablePtr。

So you need call delete oldTablePtr, that remove this widget from list of childs of parent, and move newTablePtr to the same layout. 所以你需要调用delete oldTablePtr,从父类的子列表中删除这个小部件,并将newTablePtr移动到同一个布局。

There's no need to replace it in code, you can do it in Qt Designer. 没有必要在代码中替换它,你可以在Qt Designer中完成。 Just place QTableWidget on form, then rightclick it and choose Promote widget in menu, then you will need just enter your classname. 只需将QTableWidget放在表单上,​​然后右键单击它并在菜单中选择Promote widget ,那么您只需输入您的类名即可。

Currently I don't have Qt Designer near me, so edits will be appreciated. 目前我没有Qt Designer在我附近,所以编辑将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM