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 draw rectitems with mouse
QtWS25 Last Chance

How to draw rectitems with mouse

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 5.9k Views
  • 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.
  • P Offline
    P Offline
    potla11
    wrote on last edited by
    #1

    hi..i want to draw rectitems on the scene with mouse ... i tried like this
    @
    void scene::mousePressEvent(QGraphicsSceneMouseEvent* event) {

    if (!rubberband)

    rubberband = new QRubberBand(QRubberBand::Rectangle, this);

    startPoint = event->pos();

    rubberband->setGeometry(QRect(startPoint,QSize()));

    rubberband->show();

    rubberBandActive = true;

    }

    void scene::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {

    if(rubberBandActive){

    rubberband->setGeometry(QRect(startPoint,event->pos()).normalized());

    }

    }

    void scene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {

    if(rubberBandActive){

    endPoint = event->pos();

    rubberBandActive = false;

    rubberband->hide();

    }

    }@

    i am not able to draw the rect item with this code can some one tell me how to draw rectitem with mouse

    [edit: please add @ code tags to make your code more readable, Eddy]

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p-himik
      wrote on last edited by
      #2

      Please wrap your code within 'at' to make appropriate formatting.

      QRubberBand is (AFAIK) used for selecting of items, not for drawing rectangles. So it will disappear after you release left mouse button. But you are storing endPoint so you can use it along with startPoint in your paintEvent (or whatever QGraphicsScene is using) to paint desired rectangle.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        potla11
        wrote on last edited by
        #3

        paint event i wrote like but iam not getting any rect item

        void scene::paintEvent(QPaintEvent *event)

        {
        QPainter painter(this);
        painter.drawRect(QRect(startpoint,endpoint));
        }

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          Please read this part of the "forum help":http://developer.qt.nokia.com/wiki/ForumHelp#9bd9c32b79efb1b2d5b039e4d48300a9 about how to use syntax highlighting in your thread.

          Please help us to make your questions easier to read. That will attract more people to help solve your problem.

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • P Offline
            P Offline
            potla11
            wrote on last edited by
            #5

            @void scene::paintEvent(QPaintEvent *event)
            {
            QPainter painter(this);
            painter.drawRect(QRect(startpoint,endpoint));

            }
            @
            i wrriten like this but it was not adding to the scene

            1 Reply Last reply
            0
            • EddyE Offline
              EddyE Offline
              Eddy
              wrote on last edited by
              #6

              have a look at the "graphics view doc":http://doc.qt.nokia.com/4.7/graphicsview.html

              There is example code there.

              "addRect":http://doc.qt.nokia.com/4.7/qgraphicsscene.html#addRect is what you need I suppose.
              Make your scene and use one of it's addRect methods

              edit : "complete examples":http://doc.qt.nokia.com/4.7/examples-graphicsview.html can be found here.

              Qt Certified Specialist
              www.edalsolutions.be

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

                no,i just want to draw rectitem with mouse on the scene..

                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