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. QRubberBand

QRubberBand

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.3k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I wrote this code to paint a rectangle when I press and move the mouse but it doesn't work...I don't understand why..can you help me?

    void Widget::mousePressEvent(QMouseEvent *event)
    {
    origin = event->pos();
    if (!rubberBand)
    rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    rubberBand->setGeometry(QRect(origin, QSize()));
    rubberBand->show();
    }

    void Widget::mouseMoveEvent(QMouseEvent *event)
    {
    rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
    }

    void Widget::mouseReleaseEvent(QMouseEvent *event)
    {
    rubberBand->hide();
    // determine selection, for example using QRect::intersects()
    // and QRect::contains().
    }

    raven-worxR 1 Reply Last reply
    0
    • SGaistS SGaist

      You forgot to initialise your QRubberBand variable to nullptr in the constructor.

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #10

      @SGaist thanks

      1 Reply Last reply
      0
      • ? A Former User

        I wrote this code to paint a rectangle when I press and move the mouse but it doesn't work...I don't understand why..can you help me?

        void Widget::mousePressEvent(QMouseEvent *event)
        {
        origin = event->pos();
        if (!rubberBand)
        rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
        rubberBand->setGeometry(QRect(origin, QSize()));
        rubberBand->show();
        }

        void Widget::mouseMoveEvent(QMouseEvent *event)
        {
        rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
        }

        void Widget::mouseReleaseEvent(QMouseEvent *event)
        {
        rubberBand->hide();
        // determine selection, for example using QRect::intersects()
        // and QRect::contains().
        }

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #2

        @vale88 said in QRubberBand:

        but it doesn't work

        what happens instead?
        Are you sure the mouseMoveEvent() event handler is called?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        ? 1 Reply Last reply
        0
        • raven-worxR raven-worx

          @vale88 said in QRubberBand:

          but it doesn't work

          what happens instead?
          Are you sure the mouseMoveEvent() event handler is called?

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #3

          @raven-worx nothing happends ..I don't understand..I move the mouse but nothing happends

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

            Hi,

            That might sound silly but are you first pressing one of the mouse button ?

            To ensure that your methods are called you can put qDebug statements in each of them.

            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
            • SGaistS SGaist

              Hi,

              That might sound silly but are you first pressing one of the mouse button ?

              To ensure that your methods are called you can put qDebug statements in each of them.

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #5

              @SGaist event are void function...yes,I pres the button of the mouse

              SGaistS 1 Reply Last reply
              0
              • ? A Former User

                @SGaist event are void function...yes,I pres the button of the mouse

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

                @vale88 said in QRubberBand:

                @SGaist event are void function...yes,I pres the button of the mouse

                What do you mean by event are void function ? And were is it related to my suggestion of putting qDebug statements in these functions to check what is happening ?

                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
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #7

                  Events work with qDebug().
                  But when insert code of rectangular, it doesn't work.
                  There is a crash in the application.

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #8

                    Thisi is my code .cpp :

                    #include "mainwindow.h"
                    #include "ui_mainwindow.h"
                    #include <QRubberBand>
                    #include <QMouseEvent>

                    MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                    {
                    ui->setupUi(this);
                    }

                    MainWindow::~MainWindow()
                    {
                    delete ui;
                    }

                    void MainWindow::mousePressEvent(QMouseEvent * event)

                    {

                    origin = event->pos();
                       if (!rubberBand)
                           rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
                       rubberBand->setGeometry(QRect(origin, QSize(20,10)));
                       rubberBand->show();
                    

                    ui->textBrowser->append("press");

                    }

                    void MainWindow::mouseReleaseEvent(QMouseEvent *event)
                    {
                    rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
                    ui->textBrowser->append("release");
                    }

                    void MainWindow::mouseMoveEvent(QMouseEvent *event)
                    {
                    rubberBand->hide();
                    ui->textBrowser->append("move");
                    }

                    And .h :

                    #ifndef MAINWINDOW_H
                    #define MAINWINDOW_H

                    #include <QMainWindow>
                    #include <QRubberBand>

                    namespace Ui {
                    class MainWindow;
                    }

                    class MainWindow : public QMainWindow
                    {
                    Q_OBJECT

                    public:
                    explicit MainWindow(QWidget *parent = nullptr);
                    ~MainWindow();
                    void mousePressEvent(QMouseEvent * event);
                    void mouseMoveEvent(QMouseEvent * event);
                    void mouseReleaseEvent(QMouseEvent * event);

                    private:
                    Ui::MainWindow *ui;
                    QPoint origin;
                    QRubberBand *rubberBand;
                    };

                    #endif // MAINWINDOW_H

                    I don't know if something there isn't.

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

                      You forgot to initialise your QRubberBand variable to nullptr in the constructor.

                      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
                      4
                      • SGaistS SGaist

                        You forgot to initialise your QRubberBand variable to nullptr in the constructor.

                        ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #10

                        @SGaist thanks

                        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