Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error using foreach
Forum Updated to NodeBB v4.3 + New Features

Error using foreach

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    As already suggested, just cast it in the loop. Otherwise you have to add your own method to your class that returns that list of your subclass.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    2
    • F Offline
      F Offline
      frnklu20
      wrote on last edited by
      #6

      how can i do this?

      sorry, i don't what it is

      aha_1980A 1 Reply Last reply
      0
      • F frnklu20

        how can i do this?

        sorry, i don't what it is

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #7

        @frnklu20

        Could be something like this:

        foreach(QGraphicsItem *item,scene->items()){
            DiagramItem *itempos = (DiagramItem *)item;
            ...
        }
        

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • F Offline
          F Offline
          frnklu20
          wrote on last edited by
          #8

          oh thanks :))

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #9

            You also have qgraphicsitem_cast. Take a look at the documentation. There's on additional thing you have to do before using it.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3
            • F Offline
              F Offline
              frnklu20
              wrote on last edited by frnklu20
              #10
                                        if(item->type()==DiagramItem::Type && itempos->type()==DiagramItem::Type
                                                && itempos!=item){
                                        DiagramItem *itempos = (DiagramItem *)item;
                                        DiagramItem *item = (DiagramItem *)item;
              
                                        DiagramItem *startItem = qgraphicsitem_cast<DiagramItem *>(item);
                                        DiagramItem *endItem = qgraphicsitem_cast<DiagramItem *>(itempos);
                                        Arrow *arrow = new Arrow(startItem, endItem);
                                        arrow->setColor(scene->myLineColor);
                                        item->addArrow(arrow);
                                        itempos->addArrow(arrow);
                                        arrow->setZValue(-1000.0);
                                        scene->addItem(arrow);
                                        }
              

              like this? the program keep crashing when i do this

              aha_1980A 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #11

                You're not checking whether these pointers are nullptr.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  frnklu20
                  wrote on last edited by
                  #12
                             if(item->type()==DiagramItem::Type && itempos->type()==DiagramItem::Type
                                   && itempos!=item && item!=nullptr && itempos!=nullptr){
                  

                  like this? the program still crashes

                  but if i remove the lines:

                               item->addArrow(arrow);
                               itempos->addArrow(arrow);
                  

                  it doesn't crashes but it doesn't draw a line between the objects neither

                  1 Reply Last reply
                  0
                  • F frnklu20
                                              if(item->type()==DiagramItem::Type && itempos->type()==DiagramItem::Type
                                                      && itempos!=item){
                                              DiagramItem *itempos = (DiagramItem *)item;
                                              DiagramItem *item = (DiagramItem *)item;
                    
                                              DiagramItem *startItem = qgraphicsitem_cast<DiagramItem *>(item);
                                              DiagramItem *endItem = qgraphicsitem_cast<DiagramItem *>(itempos);
                                              Arrow *arrow = new Arrow(startItem, endItem);
                                              arrow->setColor(scene->myLineColor);
                                              item->addArrow(arrow);
                                              itempos->addArrow(arrow);
                                              arrow->setZValue(-1000.0);
                                              scene->addItem(arrow);
                                              }
                    

                    like this? the program keep crashing when i do this

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @frnklu20 Where does it crash exactly? Use a debugger and step through to find the faulty line.

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      frnklu20
                      wrote on last edited by
                      #14

                      what i'm trying to do is drawing a line that is QGraphicsItem between 2 objects that already exist using the DiagramScene example form Qt

                      diagramscene.cpp

                      void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
                      {
                             
                          if (mouseEvent->button() != Qt::LeftButton)
                              return;
                      
                          DiagramItem *item;
                          switch (myMode) {
                                 case InsertLine:
                                  line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),
                                                              mouseEvent->scenePos()));
                                  line->setPen(QPen(myLineColor, 2));
                                  addItem(line);
                                  break;
                      }
                      }
                      
                      void DiagramScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
                      {
                          if (myMode == InsertLine && line != 0) {
                              QLineF newLine(line->line().p1(), mouseEvent->scenePos());
                              line->setLine(newLine);
                          } else if (myMode == MoveItem) {
                              QGraphicsScene::mouseMoveEvent(mouseEvent);
                          }
                      }
                      
                      void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
                      {
                          if (line != 0 && myMode == InsertLine) {
                              QList<QGraphicsItem *> startItems = items(line->line().p1());
                              if (startItems.count() && startItems.first() == line)
                                  startItems.removeFirst();
                              QList<QGraphicsItem *> endItems = items(line->line().p2());
                              if (endItems.count() && endItems.first() == line)
                                  endItems.removeFirst();
                      
                              removeItem(line);
                              delete line;
                      
                      
                              if (startItems.count() > 0 && endItems.count() > 0 &&
                                  startItems.first()->type() == DiagramItem::Type &&
                                  endItems.first()->type() == DiagramItem::Type &&
                                  startItems.first() != endItems.first()) {
                                  DiagramItem *startItem = qgraphicsitem_cast<DiagramItem *>(startItems.first());
                                  DiagramItem *endItem = qgraphicsitem_cast<DiagramItem *>(endItems.first());
                                  Arrow *arrow = new Arrow(startItem, endItem);
                                  arrow->setColor(myLineColor);
                                  startItem->addArrow(arrow);
                                  endItem->addArrow(arrow);
                                  arrow->setZValue(-1000.0);
                                  addItem(arrow);  #and here it draws the line
                                  arrow->updatePosition()
                      }
                      }
                      

                      and i what i want to draw a line between 2 objects but not by creating a line with insertline mode

                      mainwindow.cpp

                      void MainWindow::infoItem()
                      {
                          foreach (QGraphicsItem *item, scene->selectedItems()){#the item object is the selected one
                             if (item->type() == DiagramItem::Type){
                      
                      if(item->data(0)=="Carga"){#carga is type of object
                      CargaDialog *carga=new CargaDialog;
                                    carga->setModal(true);
                      #its a dialog that contains a combo box, an the items of this combo box are the items that are in the screen that are not cargas
                      #and after select an item in the combo box, it will draw a line between the carga and the item selected
                      
                      if(carga->getItem()!="Selecione um barramento" && carga->getItem()!=(item->data(1)).toString()){
                                        #getItem() is a function that return the name of item selected in the combo box
                                        item->setData(4,carga->getItem());
                                        foreach(QGraphicsItem *itempos,scene->items()){
                                            if(itempos->data(1)==carga->getItem())
                                           #i use the data(1) slot of the objects to store the name of each object, so now i looking for the QGraphicsItem that have the same name as the item selected
                                            {
                      
                                                if(item->type()==DiagramItem::Type && itempos->type()==DiagramItem::Type
                                                        && itempos!=item && item!=nullptr && itempos!=nullptr){
                                                DiagramItem *itempos = (DiagramItem *)item;
                                                DiagramItem *item = (DiagramItem *)item;
                      
                                                DiagramItem *startItem = qgraphicsitem_cast<DiagramItem *>(item);
                                                DiagramItem *endItem = qgraphicsitem_cast<DiagramItem *>(itempos);
                                                Arrow *arrow = new Arrow(startItem, endItem);
                                                arrow->setColor(scene->myLineColor);
                                                item->addArrow(arrow);#if i remove these two lines, the program doesn't crash but it doesn't draw a line on the screen
                                                itempos->addArrow(arrow);
                                                arrow->setZValue(-1000.0);
                                                scene->addItem(arrow);
                                                arrow->updatePosition();
                                                }
                                            }
                      

                      i don't know why this is going on, because it's exactaly the same this that is done in diagramscene.cpp

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        As already suggested, start your application with the debugger and check the stack trace.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • fcarneyF Offline
                          fcarneyF Offline
                          fcarney
                          wrote on last edited by
                          #16

                          @frnklu20 said in Error using foreach:

                          (DiagramItem *)item;

                          Should this be done this way?

                          DiagramItem *itempos = dynamic_cast<DiagramItem*>(item);
                          // check if itempos is null
                          

                          This gives us some more type safety no?

                          C++ is a perfectly valid school of magic.

                          1 Reply Last reply
                          2

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved