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. How to get clicked item index of QScrollArea?
Forum Updated to NodeBB v4.3 + New Features

How to get clicked item index of QScrollArea?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 845 Views 3 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.
  • B Offline
    B Offline
    BushyAxis793
    wrote on last edited by
    #1

    Hello. I have a problem. How can I get an index of item in QScrollArea?

    This is my code:

        QWidget *widget = new QWidget();
        ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        ui->scrollArea->setWidgetResizable(true);
        ui->scrollArea->setWidget(widget);
        widget->setLayout(Vlayout);
    

    I have some custom widgets added to QVBoxLayout:

    Vlayout->addWidget(operationWidget);
    

    Result:
    9aa3a544-c7fe-4a95-ace2-9343f8e7af00-image.png

    And I would like to set each of my widget as checked (highlighted). So I decided to use event filter:

    ui->scrollArea->viewport()->installEventFilter(this);
    
    bool QueriesCreator::eventFilter(QObject *object, QEvent *event)
    {
    if(object == ui->scrollArea->viewport() && event->type()==QEvent::MouseButtonRelease)
        {
            QMouseEvent * mouseEvent = static_cast< QMouseEvent * >( event );
    
            if( mouseEvent->button() == Qt::LeftButton ) {
    
                if(Vlayout->count()>0)
                {
                    QWidget *widget = childAt(mouseEvent->pos());
    
    
                    int index = Vlayout->layout()->indexOf(widget);
                    qDebug()<<index;
                    if(index>=0)
                    {
                        QLayoutItem* const item = Vlayout->layout()->itemAt(index);
                        auto widget = dynamic_cast<OperationWidget*>(item);
                        
                    }
                }
    
                return true;
            }
            else
                return false;
        }
    
        return false;
    
    }
    

    And the index I get is -1. I don't know why. If you have any idea how to get index of clicked QScrollArea element. I will be very thankfull. Have a good day.

    SGaistS Chris KawaC 2 Replies Last reply
    0
    • B BushyAxis793

      Hello. I have a problem. How can I get an index of item in QScrollArea?

      This is my code:

          QWidget *widget = new QWidget();
          ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
          ui->scrollArea->setWidgetResizable(true);
          ui->scrollArea->setWidget(widget);
          widget->setLayout(Vlayout);
      

      I have some custom widgets added to QVBoxLayout:

      Vlayout->addWidget(operationWidget);
      

      Result:
      9aa3a544-c7fe-4a95-ace2-9343f8e7af00-image.png

      And I would like to set each of my widget as checked (highlighted). So I decided to use event filter:

      ui->scrollArea->viewport()->installEventFilter(this);
      
      bool QueriesCreator::eventFilter(QObject *object, QEvent *event)
      {
      if(object == ui->scrollArea->viewport() && event->type()==QEvent::MouseButtonRelease)
          {
              QMouseEvent * mouseEvent = static_cast< QMouseEvent * >( event );
      
              if( mouseEvent->button() == Qt::LeftButton ) {
      
                  if(Vlayout->count()>0)
                  {
                      QWidget *widget = childAt(mouseEvent->pos());
      
      
                      int index = Vlayout->layout()->indexOf(widget);
                      qDebug()<<index;
                      if(index>=0)
                      {
                          QLayoutItem* const item = Vlayout->layout()->itemAt(index);
                          auto widget = dynamic_cast<OperationWidget*>(item);
                          
                      }
                  }
      
                  return true;
              }
              else
                  return false;
          }
      
          return false;
      
      }
      

      And the index I get is -1. I don't know why. If you have any idea how to get index of clicked QScrollArea element. I will be very thankfull. Have a good day.

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are likely getting one of the subwidget of your "items".

      From the looks of it, using a QListView with a QStyledItemDelegate would be simpler and you would have the highlighting easier to handle.

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

      B 1 Reply Last reply
      1
      • B BushyAxis793

        Hello. I have a problem. How can I get an index of item in QScrollArea?

        This is my code:

            QWidget *widget = new QWidget();
            ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
            ui->scrollArea->setWidgetResizable(true);
            ui->scrollArea->setWidget(widget);
            widget->setLayout(Vlayout);
        

        I have some custom widgets added to QVBoxLayout:

        Vlayout->addWidget(operationWidget);
        

        Result:
        9aa3a544-c7fe-4a95-ace2-9343f8e7af00-image.png

        And I would like to set each of my widget as checked (highlighted). So I decided to use event filter:

        ui->scrollArea->viewport()->installEventFilter(this);
        
        bool QueriesCreator::eventFilter(QObject *object, QEvent *event)
        {
        if(object == ui->scrollArea->viewport() && event->type()==QEvent::MouseButtonRelease)
            {
                QMouseEvent * mouseEvent = static_cast< QMouseEvent * >( event );
        
                if( mouseEvent->button() == Qt::LeftButton ) {
        
                    if(Vlayout->count()>0)
                    {
                        QWidget *widget = childAt(mouseEvent->pos());
        
        
                        int index = Vlayout->layout()->indexOf(widget);
                        qDebug()<<index;
                        if(index>=0)
                        {
                            QLayoutItem* const item = Vlayout->layout()->itemAt(index);
                            auto widget = dynamic_cast<OperationWidget*>(item);
                            
                        }
                    }
        
                    return true;
                }
                else
                    return false;
            }
        
            return false;
        
        }
        

        And the index I get is -1. I don't know why. If you have any idea how to get index of clicked QScrollArea element. I will be very thankfull. Have a good day.

        Chris KawaC Online
        Chris KawaC Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @BushyAxis793 You're probably clicking on a sub widget, so indexOf will not find it. You can find your widget easier by going through the parents of what was clicked and avoiding the indexing and dynamic casts:

        ...
        QWidget *widget = childAt(mouseEvent->pos());
        
        // go up parent chain until you find OperationWidget
        while(widget && !widget->metaObject()->inherits(&OperationWidget::staticMetaObject))
             widget = widget->parentWidget();
        
        if (widget)
        {
           OperationWidget* op = qobject_cast<OperationWidget*>(widget);
           // do something with op
        }
        else
        {
           // not found, probably clicked between widgets
        }
        
        JonBJ 1 Reply Last reply
        1
        • Chris KawaC Chris Kawa

          @BushyAxis793 You're probably clicking on a sub widget, so indexOf will not find it. You can find your widget easier by going through the parents of what was clicked and avoiding the indexing and dynamic casts:

          ...
          QWidget *widget = childAt(mouseEvent->pos());
          
          // go up parent chain until you find OperationWidget
          while(widget && !widget->metaObject()->inherits(&OperationWidget::staticMetaObject))
               widget = widget->parentWidget();
          
          if (widget)
          {
             OperationWidget* op = qobject_cast<OperationWidget*>(widget);
             // do something with op
          }
          else
          {
             // not found, probably clicked between widgets
          }
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Chris-Kawa said in How to get clicked item index of QScrollArea?:

          !widget->metaObject()->inherits(&OperationWidget::staticMetaObject)

          What's this bit Chris? I can guess this is how you recognise the root of the widget tree?

          Chris KawaC 1 Reply Last reply
          0
          • JonBJ JonB

            @Chris-Kawa said in How to get clicked item index of QScrollArea?:

            !widget->metaObject()->inherits(&OperationWidget::staticMetaObject)

            What's this bit Chris? I can guess this is how you recognise the root of the widget tree?

            Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            @JonB The loop is pretty much just "while widget is not OperationWidget check parent".
            This

            widget->metaObject()->inherits(&OperationWidget::staticMetaObject)
            

            is pretty much the same as

            dynamic_cast<OperationWidget*>(widget) != nullptr
            

            just QObject specific. I guess you can also write that loop as

            while (widget && !qobject_cast<OperationWidget*>(widget))
            

            if you don't fancy the metaObject stuff directly (qobject_cast does that internally).

            M 1 Reply Last reply
            1
            • Chris KawaC Chris Kawa

              @JonB The loop is pretty much just "while widget is not OperationWidget check parent".
              This

              widget->metaObject()->inherits(&OperationWidget::staticMetaObject)
              

              is pretty much the same as

              dynamic_cast<OperationWidget*>(widget) != nullptr
              

              just QObject specific. I guess you can also write that loop as

              while (widget && !qobject_cast<OperationWidget*>(widget))
              

              if you don't fancy the metaObject stuff directly (qobject_cast does that internally).

              M Offline
              M Offline
              mpergand
              wrote on last edited by
              #6

              @Chris-Kawa said in How to get clicked item index of QScrollArea?:

              widget->metaObject()->inherits(&OperationWidget::staticMetaObject)

              or
              widget->inherits("OperationWidget")

              Chris KawaC 1 Reply Last reply
              1
              • M mpergand

                @Chris-Kawa said in How to get clicked item index of QScrollArea?:

                widget->metaObject()->inherits(&OperationWidget::staticMetaObject)

                or
                widget->inherits("OperationWidget")

                Chris KawaC Online
                Chris KawaC Online
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @mpergand I have an acquired distaste for string based APIs, so I intentionally try not to mention them whenever I can :) But yeah, you could do that and then rename the class while forgetting to also change the string and wonder what's happening. To each their own :)

                M 1 Reply Last reply
                1
                • Chris KawaC Chris Kawa

                  @mpergand I have an acquired distaste for string based APIs, so I intentionally try not to mention them whenever I can :) But yeah, you could do that and then rename the class while forgetting to also change the string and wonder what's happening. To each their own :)

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by
                  #8

                  @Chris-Kawa said in How to get clicked item index of QScrollArea?:

                  I have an acquired distaste for string based APIs,

                  OK, cut the apple in two :)
                  widget->inherits(OperationWidget::staticMetaObject.className())

                  Anyway, I don't see any reason for not using qobject_cast here.

                  JonBJ 1 Reply Last reply
                  0
                  • M mpergand

                    @Chris-Kawa said in How to get clicked item index of QScrollArea?:

                    I have an acquired distaste for string based APIs,

                    OK, cut the apple in two :)
                    widget->inherits(OperationWidget::staticMetaObject.className())

                    Anyway, I don't see any reason for not using qobject_cast here.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @mpergand said in How to get clicked item index of QScrollArea?:

                    Anyway, I don't see any reason for not using qobject_cast here.

                    +1 :)

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Hi,

                      You are likely getting one of the subwidget of your "items".

                      From the looks of it, using a QListView with a QStyledItemDelegate would be simpler and you would have the highlighting easier to handle.

                      B Offline
                      B Offline
                      BushyAxis793
                      wrote on last edited by
                      #10

                      @SGaist I supposed it too. However here: https://forum.qt.io/topic/142288/how-can-i-add-custom-widget-to-qlistview/15

                      I tried to create model/view but without result. So I decided to looking for new ideas which would be good for me. All I want is some features:

                      • Highlight my widget
                      • Double clik on my widget
                      • Change order (move up/down) of my witgets list

                      I hope I will find solution.

                      SGaistS 1 Reply Last reply
                      0
                      • B BushyAxis793

                        @SGaist I supposed it too. However here: https://forum.qt.io/topic/142288/how-can-i-add-custom-widget-to-qlistview/15

                        I tried to create model/view but without result. So I decided to looking for new ideas which would be good for me. All I want is some features:

                        • Highlight my widget
                        • Double clik on my widget
                        • Change order (move up/down) of my witgets list

                        I hope I will find solution.

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        You do realize that you are trying to reimplement the model/view paradigm rather than implementing a delegate that should paint three strings ?

                        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

                        • Login

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