0

I want to show a wide range of the same graphic items into one scene. All items are lines. Some lines are at the same horizontal position. To manage this I create organizeItemGroups. The universal function sorted the data of an item into a QGraphicsItemGroup. To show these groups I use QCheckbox. If a checkbox item is clicked the corresponding slot paintLinesAudCtrl1HD calls organizeItemGroups. With this code it is possible to show line items that are sorted in defined categories. I also want to hide the groups/lineItems and that is not possible. I think the problem is that in organizeItemGroups I create multiple objects of QGraphicsItemGroup and that I only hide one instance of the interface in paintLinesAudCtrl1HD. I also try to remove the group from the scene and delete the instance but this produces an error.

Can someone explain me if there is a solution for this? I'am on a junior level, so please let me know if you need more information.

EDIT: Please not that paintLinesAudCtrl1HD represents one category. it draw line at position P(0, 9, 80, 9) and P(0, 571, 80, 571)

/**
 * this function draws lines into the scene and is called if a checkbox item is clicked.
 */
void MagnifierScreen::paintLinesAudCtrl1HD(bool checked)
{
    QGraphicsItemGroup* audioCtrl1HD = new QGraphicsItemGroup;
    if (checked) {
        organizeItemGroups(ancName.audioCtrlPacketGroup1HD, ancColor.audioColorGroup1, audioCtrl1HD);
        qDebug() << "checked";
    }
    else if (!checked) {
        audioCtrl1HD->hide();
        qDebug() << "unchecked";
    }
}
            
            
/**
 * line items are organized in groups and this groups are added to a scene.
 * In the program the user can hide this groups to show new data.
 */
void MagnifierScreen::organizeItemGroups(QString packetName, QColor lineColor, QGraphicsItemGroup* obj)
{
    for (int i = 0; i < m_lineData.count(); ++i) {
        QGraphicsItemGroup* obj = new QGraphicsItemGroup;
        if (m_lineData.at(i).name == packetName) {
            //defines a line item that is derived from QGraphicsItem
            LineItem* lineItem = new LineItem(m_lineData.at(i).name, lineColor, m_lineData.at(i).line);
            obj->addToGroup(lineItem);
            qDebug() << "organizeItemGroups called." << lineItem->getName() << lineItem->getColor() << lineItem->getLine();
        }
        m_scene->addItem(obj);
    }
}
    
/**
 * stores line items in a QList. A line item is a tupel of the name of data and the number of the line.
 */
void MagnifierScreen::addLinesToList(QString name, int line)
{
    LineData lineData;
    lineData.name = name;
    lineData.line = line;
    m_lineData.push_back(lineData);
}
taathy
  • 155
  • 1
  • 1
  • 9
  • It looks like that the object of QGraphicsItemGroup have to be created in the constructor of the class and not in the loop. That fixed the issue. Now it works. – taathy Sep 20 '22 at 13:56

0 Answers0