简体   繁体   English

QTreeWidget中项目的ID已更改

[英]Id of an item in QTreeWidget changed

There is something very strange about Qt. Qt有点奇怪。

I have a button ui->addPointButton and a QtreeWidget ui->pointListBox . 我有一个按钮ui->addPointButton和一个QtreeWidget ui->pointListBox When I click on the button, it adds a point to the tree. 当我单击按钮时,它会在树上添加一个点。 mScenePtr is a pointer to the class I put all my points. mScenePtr是指向我提出的所有观点的类的指针。 AddPoint is a class creating a window asking for some information about the point. AddPoint是一个类,它创建一个窗口,询问有关该点的一些信息。

void AddPointsWindow::on_addPointButton_clicked(bool clicked)
{
    Q_UNUSED(clicked);

    AddPoint addPointWindow(mScenePtr->getColor_or_texture());
    int addPointWindowResult = addPointWindow.exec();

    if  (addPointWindowResult == QDialog::Accepted)
    {
        SVertex vertex = addPointWindow.getVertex();
        mScenePtr->addVertex(vertex);

        QTreeWidgetItem* itemPtr = new QTreeWidgetItem(ui->pointListBox);
        cout << "id" << ui->pointListBox->indexOfTopLevelItem(itemPtr) << endl;

        //itemPtr->setText(0,QString::number(mScenePtr->getVertexNumber()));
        //itemPtr->setText(0, QString::number(ui->pointListBox->indexOfTopLevelItem(itemPtr)));
        itemPtr->setText(0, "hjhjh");
        cout << "id" << ui->pointListBox->indexOfTopLevelItem(itemPtr) << endl;

        itemPtr->setText(1, QString::number(vertex.x));
        itemPtr->setText(2, QString::number(vertex.y));
        itemPtr->setText(3, QString::number(vertex.z));
        if (color == mScenePtr->getColor_or_texture())
        {
            itemPtr->setText(4, QString::number(vertex.r));
            itemPtr->setText(5, QString::number(vertex.g));
            itemPtr->setText(6, QString::number(vertex.b));
        }
        //ui->pointListBox->insertTopLevelItem(ui->pointListBox->topLevelItemCount(), itemPtr);



        cout << "value : " << vertex.x << endl;

    }
}

In this exemple, I click twice on the buttons, create two points with vertex.x = 0 for the first and 1 for the second. 在此示例中,我在按钮上单击两次,创建两个点,其中vertex.x第一个为vertex.x = 0,第二个为1。

Look at the three lines in the middle : 看一下中间的三行:

            //itemPtr->setText(0,QString::number(mScenePtr->getVertexNumber()));
        //itemPtr->setText(0, QString::number(ui->pointListBox->indexOfTopLevelItem(itemPtr)));
        itemPtr->setText(0, "hjhjh");

If there is only the third line, the result is 如果只有第三行,则结果为

id0
id0
value : 0
id1
id1
value : 1

Everythong is ok. 一切都很好。 But if I put one of the two others lines, the result in both cases is : 但是,如果我放置其他两行之一,则两种情况的结果都是:

id0
id0
value : 0
id1
id0
value : 1

How is it possible ? 这怎么可能 ? How can the call to ui->pointListBox->indexOfTopLevelItem(itemPtr) or mScenePtr->getVertexNumber() can change the id of the item ? 如何调用ui->pointListBox->indexOfTopLevelItem(itemPtr)mScenePtr->getVertexNumber()可以更改项目的ID?

Qt 5.5 Qt 5.5

After using setText , the items in the tree might have sorted automatically. 使用setText ,树中的项目可能已自动排序。

So in the two commented line cases, when you add the number (using setText ), the nodes are getting sorted and the earlier node has become the top level item. 因此,在两种带注释的行情况下,当您添加数字(使用setText )时,将对节点进行排序,并且较早的节点已成为顶级项。

That is the reason you are seeing two different IDs "before setText " and "after setText ", when you are querying "top level item". 这就是在查询“顶级项目”时看到“ setText之前”和“ setText之后”两个不同的ID的原因。

To see the results properly, Turn off the sorting for the tree. 要正确查看结果,请关闭树的排序。 (may be in your constructor) (可能在您的构造函数中)

ui->pointListBox->setSortingEnabled(false);

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

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