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. Push button while dragging mouse

Push button while dragging mouse

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 5.6k Views 1 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.
  • M Offline
    M Offline
    mrcgnerd
    wrote on last edited by
    #1

    Hello
    Let say I have 5 qpushbuttons placed in one row. I can press them separatelty one by one, but what if I want to triger them while draging the mouse ? It looks like this: I press one button and while holding left button drag mouse to other buttons in the row and they sould react like I pressed them. How can i do that ?
    Thank you !
    Sorry for my english

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      a little weird but you can just calculate yourself if the dragging button is above another one with simple rectangle collisions and then call the click slot of the button?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrcgnerd
        wrote on last edited by
        #3

        Thanks, but I don't move button by it selve.
        For example each button changes color to yellow when it's clicked.
        Now I press first button in the row and drag mouse cursor (not Pushbutton), to the right over other buttons and all of them changing collors yellow.

        I think I could calculate cursor posittion. but I dont know how ?

        Sorry for my english.

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on last edited by
          #4

          yeah sorry for misunderstanding, if you just move the cursor and not the button that is the same, just easier! :)
          You could override this method in the widget or layout where the buttons are:
          @
          QWidget::mouseMoveEvent(QMouseEvent * event)
          @
          and then you should get mouse move events when you press the button and move the mouse, just try it.
          I don't think it will be hard to find out if the mouse is over another button for you? :)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrcgnerd
            wrote on last edited by
            #5

            Thank you.

            I just tested QMouseEvent and I don't know why but it's not working as I expected. My code:

            @void MainWindow::mouseMoveEvent(QMouseEvent * event)
            {
            if (ui->Button->rect().contains(event->pos()))
            {
            ui->Button->setText("YES");}
            else{ui->Button->setText("NO");}
            }@

            And I always get text "NO" no matter where cursor is, inside or outside of button.

            EDIT

            Looks like I am getting wrong position of cursor, or PushButton, Because I get text setted to "YES" when currsor is somwhere above button but not inside.

            1 Reply Last reply
            0
            • X Offline
              X Offline
              Xander84
              wrote on last edited by
              #6

              I think the mouse pos might be in global coordinates, but the button pos relative to the parent.
              Try and print the positions to the console...

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrcgnerd
                wrote on last edited by
                #7

                Thanks again.
                Yes it was set to global coordinates, I fixed it like this:
                @QRect widgetRect = ui->Btt_1->geometry();
                widgetRect.moveTopLeft(ui->Btt_1->parentWidget()->mapToGlobal(widgetRect.topLeft()));
                @
                Now position is correct.

                I don't want to rewrite same thing for each button so I am trying to write function. But I can't pass QPushButton to the function. I am trying to do this:

                @void MainWindow::mouseMoveEvent(QMouseEvent * event, QPushButton *btn)
                {
                QRect widgetRect = btn->geometry();
                widgetRect.moveTopLeft(btn->parentWidget()->mapToGlobal(widgetRect.topLeft()));

                if (widgetRect.contains(QCursor::pos())){btn->clicked();}
                

                }@
                but when I call that function I am getting error.

                @mouseMoveEvent(ui->Button_1); //Error no matching function for call to 'MainWindow::mouseMoveEvent(QPushButton*&)'@

                How can I pass PushButton corectly to function ?

                Sory for my english.

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  Xander84
                  wrote on last edited by
                  #8

                  you can't simply change the signature of the mouseMoveEvent function and add a parameter to it, then it won't work as you expect because QWidget won't call it and nothing happens.

                  I think you have multiple buttons you need to check? you could just do a for-loop in the mouseMoveEvent function to check all buttons and don't need an extra function then, but I don't know more about you app so it's hard to say what would be best in your case.

                  Where do you call "mouseMoveEvent(ui->Button_1);" in your code?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mrcgnerd
                    wrote on last edited by
                    #9

                    Sory I didn't got atime to reply.

                    Yes I have multiple buttons to check.
                    As far as I understund I can't loop UI element.

                    @for (i = 1; i < ButtnCount; i++)
                    {
                    QRect widgetRect = ui->PUSHBUTTON_i->geometry();
                    widgetRect.moveTopLeft(ui->PUSHBUTTON_i->parentWidget()->mapToGlobal(widgetRect.topLeft()));

                    if (widgetRect.contains(QCursor::pos())){ui->PUSHBUTTON_i->clicked();
                    

                    }@

                    I know I can write a QVector:
                    @
                    QVector <QPushButton*> ButtonVector;
                    ButtonVector.push_back (ui->PUSHBUTTON_1);
                    ButtonVector.push_back (ui->PUSHBUTTON_2);
                    ButtonVector.push_back (ui->PUSHBUTTON_3);
                    ...................
                    @
                    And it's ok for this example, where I have 5 buttons, but what if I have more then that like 200 or so, then I have to write every button in QVector.

                    My program works like thsis each button can have two statets - marked (RED) and unmarked (GREEN):
                    @void MainWindow::on_PUSHBUTTON_1_clicked()
                    {
                    if (PUSHBUTTON_1->stylesheet = "Code for RED color"){
                    ui->PUSHBUTTON_1->setstylesheet = "Code for GREEN color";
                    else ui->PUSHBUTTON_1->setstylesheet = "Code for RED color";}
                    }@
                    I repeated it for all 5 buttons. By the way is there a way to avoid that ? can I find out if any of five buttons was pressed and wich ? and than do something.

                    Then my program works ok, I can press button and it will change it state. But I want to have abilaty to do that not just clicking on button but draging a mouse over few buttons and changing it's state, lets call it "Paint select".

                    Sorry for my english

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      Xander84
                      wrote on last edited by
                      #10

                      Hi, I guess you have all the buttons in a layout (QLayout)? then you can iterate though the layout items and get a list of all the button, without to manually add them to a list (or vector) or whatever.
                      If you only have buttons in that layout it is very easy, if you have other widgets as well you need a filter which is not that nice in c++ because you have to dynamically cast the item to see if it actually is a button or something else:
                      @
                      QLayout *layout = ui->yourButtonLayout;
                      for (int i = 0; i < layout->count(); ++i) {
                      QLayoutItem *layoutItem = layout->itemAt(i);
                      // check if layout widget is a button
                      QWidget widget = layoutItem->widget();
                      if (qobject_cast<QPushButton
                      >(widget)) {
                      QRect widgetRect = widget->geometry();
                      // do your cursor stuff etc
                      }
                      }
                      @
                      there are other ways how you can get your button without adding them all by yourself, but that depends on the widgets and layouts you are using, I hope this gets you started at least. :)

                      aso be careful you forgot a "=" in your if i think:
                      @
                      if (PUSHBUTTON_1->stylesheet = "Code for RED color")
                      @
                      that is an assignment and no comparison, you need to use
                      @
                      if (PUSHBUTTON_1->stylesheet == "Code for RED color")
                      @

                      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