Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved get mousePressEvent in qlistview

    General and Desktop
    4
    19
    2774
    Loading More Posts
    • 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.
    • S
      saber last edited by

      i want get the mouse pressed in blank space , where there is no item.

      my code is

      // in constructor 
      ui->viewtree->installEventFilter(this);
      
      
      void corefm::mousePressEvent(QMouseEvent *event)
      {
          qDebug() << "Mounting " ;
      
      }
      
      

      problem is i am not getting anything. what i am doing wrorng.
      note. i used a custom model to set the list item.

      here is my project COREBOX

      1 Reply Last reply Reply Quote 0
      • Ratzz
        Ratzz last edited by

        @saber

        Did you check this post ?

        --Alles ist gut.

        S 1 Reply Last reply Reply Quote 0
        • S
          saber @Ratzz last edited by

          @Ratzz it is qlist in icon view.

          i need if mouse clicked in where there is no item.

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @saber last edited by mrjj

            @saber
            Hi
            you have to set eventfiler on its viewport class.

            S 1 Reply Last reply Reply Quote 3
            • S
              saber @mrjj last edited by saber

              @mrjj i don't know what is viewport class.

              @Ratzz i tried connect method , but it only works when in click an item.

              i want a function , when i click no item or whitespace.

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @saber last edited by

                @saber
                Its sort of a inner widget that actually is covering the background of the ListView
                its the object that get events etc.
                You get get it via
                ui->listView->viewport();
                and use it in connect.

                S 1 Reply Last reply Reply Quote 1
                • S
                  saber @mrjj last edited by saber

                  @mrjj here is how i tried

                  // in constructor 
                  connect(ui->viewlist->viewport(), SIGNAL(clicked()),this, SLOT(ll()));
                  
                  void corefm::ll(){
                  
                      qDebug() << "lllllll " ;
                  }
                  

                  in application output i got this .

                  QObject::connect: No such signal QWidget::clicked() in
                  ../corebox/corefm/corefm.cpp:45
                  QObject::connect:  (sender name:   'qt_scrollarea_viewport')
                  QObject::connect:  (receiver name: 'corefm')
                  

                  and why this is not working ??

                  void corefm::mousePressEvent(QMouseEvent *event)
                  {
                      qDebug() << "Mounting " ;
                  }
                  
                  1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion last edited by mrjj

                    Hi
                    The viewport do not have a clicked() signal.
                    I meant for you use it with event filter to catch it that way.

                    S 2 Replies Last reply Reply Quote 3
                    • S
                      saber @mrjj last edited by

                      @mrjj

                      here is the 2 view for my file manager . first on is listview and second one is tree view.
                      but mouse mousePressEvent is not working.

                      here is the cpp file corefm.cpp

                      ui->viewlist->viewport()->installEventFilter(this);
                      ui->viewtree->viewport()->installEventFilter(this);
                      
                      1 Reply Last reply Reply Quote 0
                      • S
                        saber @mrjj last edited by

                        @mrjj i am getting mousePressEvent in outside of listview

                        0_1525099082598_uu.png

                        1 Reply Last reply Reply Quote 0
                        • mrjj
                          mrjj Lifetime Qt Champion last edited by

                          Hmm, it dont recall it being that difficult.
                          And the listview is all the way down there ?

                          S 1 Reply Last reply Reply Quote 0
                          • S
                            saber @mrjj last edited by saber

                            @mrjj
                            no, that is another widget.
                            i need that function . please help me

                            mrjj 1 Reply Last reply Reply Quote 0
                            • mrjj
                              mrjj Lifetime Qt Champion @saber last edited by

                              @saber
                              Well what ever widget is under the mouse cursor gets the click.
                              So you need event filter on those too.
                              I though you just want to click outside the actual items,
                              not the whole wide window.

                              S 1 Reply Last reply Reply Quote 0
                              • S
                                saber @mrjj last edited by saber

                                @mrjj
                                yes , i just want to click outside the actual items in listview .
                                i just want click event on list view .
                                not whole window.

                                0_1525160332219_Screenshot_2018-05-01_13-34-34.png

                                but mouse press event not working there.

                                1 Reply Last reply Reply Quote 0
                                • S
                                  saber last edited by

                                  any light ?

                                  1 Reply Last reply Reply Quote 0
                                  • V
                                    VRonin last edited by VRonin

                                    Try this:

                                    class ClickOutListView : public QListView{
                                    Q_OBJECT
                                    Q_DISABLE_COPY(ClickOutListView)
                                    public:
                                    explicit ClickOutListView(QWidget* parent = Q_NULLPTR) : QListView(parent){}
                                    Q_SIGNALS:
                                    void clickedOut();
                                    protected:
                                    void mousePressEvent(QMouseEvent *event){
                                    if(!indexAt(event->pos()).isValid()) 
                                    clickedOut();
                                    QListView::mousePressEvent(event);
                                    }
                                    };
                                    

                                    and right-click->promote your QListView to ClickOutListView in designer

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    S 1 Reply Last reply Reply Quote 4
                                    • S
                                      saber @VRonin last edited by

                                      @VRonin
                                      sorry for late reply .
                                      your code is working.
                                      can i use this in treewidget to achieve same mouseevent?

                                      mrjj 1 Reply Last reply Reply Quote 0
                                      • mrjj
                                        mrjj Lifetime Qt Champion @saber last edited by

                                        @saber
                                        Hi
                                        Yes QTreeWidget also have
                                        mousePressEvent etc. so should work the same.

                                        S 1 Reply Last reply Reply Quote 1
                                        • S
                                          saber @mrjj last edited by

                                          @mrjj

                                          in treeview it's also working.
                                          thanks.

                                          1 Reply Last reply Reply Quote 1
                                          • First post
                                            Last post