Results 1 to 17 of 17

Thread: Drawing a rectangle over Qlabel image

  1. #1
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default Drawing a rectangle over Qlabel image

    I have a map as a image in my Qlabel, now i want to draw a rectangle over the image how to i do it? Because at the end of the day i need the rect(which is my robot footprint to be able to move accordingly. i tried :
    MainWindow::MainWindow(Tqt_interface* tqt, QWidget *parent ) : QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QPixmap pix("/home/liqi/Qt_robotino/src/image/ariccmap.png");
    ui->labelmap->setPixmap(pix);

    QRectF rectangle(10.0, 20.0, 200.0, 200.0);
    QPainter painter(this);
    painter.drawRect(rectangle); // drawing code
    }
    but the rectangle does not appear. Any helps is appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing a rectangle over Qlabel image

    You are trying to draw on the widget instead of the pixmap.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default Re: Drawing a rectangle over Qlabel image

    Then how do I make it to draw on the image instead?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing a rectangle over Qlabel image

    Open the painter on the image and not on the widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing a rectangle over Qlabel image

    I am new Qt . I tried to draw a line on image using Qpainter on QPixmapBut image did not load just everything was black on Qlabel conataining Qpixmap;
    I dont why
    my code is
    Qt Code:
    1. QPixmap pix(500,500);
    2. ui->label->resize(pix.size());
    3. pix.load(QString::fromUtf8("C:\raster.jpg"));
    4. QPainter painter(&pix);
    5. QPen Red((QColor::QColor(255,0,0)),10);
    6. painter.setPen(Red);
    7. painter.drawLine(10,10,50,50);
    8. ui->label->setPixmap(pix);
    To copy to clipboard, switch view to plain text mode 

    problem is that raster.jpg image is not loading ....only whole black is appearing
    Last edited by wysota; 11th October 2011 at 13:10. Reason: missing [code] tags

  6. #6
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default Re: Drawing a rectangle over Qlabel image

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

    QImage image("/home/liqi/Qt_robotino/src/image/ariccmap.png");

    // tell the painter to draw on the QImage
    QPainter* painter = new QPainter(&image); // sorry i forgot the "&"
    painter->setPen(Qt::red);
    painter->setFont(QFont("Arial", 10));
    // you probably want the to draw the text to the rect of the image
    painter->drawText(image.rect(), Qt::AlignBottom, "Text on Image");

    QLabel* imageLabel = new QLabel();
    ui->labelmap->setPixmap(QPixmap::fromImage(image));
    }
    Ok, now i managed to at least get text over the image, now, how do i go about placing one image over the previous one instead of text?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing a rectangle over Qlabel image

    Such step by step approach is not a good way of solving problems. QLabel might be fit for displaying a simple pixmap however if you keep increasing complexity like that, you'll reach a state when you'll want to implement a 3d game and QLabel is not the best candidate for it. Maybe you should first say what your goal is and then we'll suggest a solution (I can already suggest using QGraphicsView instead of QLabel).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default Re: Drawing a rectangle over Qlabel image

    Okay, im actually trying to display a map on Qt, then insert a "robot footprint' on top of the map. The laptop with Qt design is client side, it will receive information from the server side(another laptop on the robot) and the robot footprint will move accordingly on the map. my server side is able to receive the x,y,z position now.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing a rectangle over Qlabel image

    Use Graphics View.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default Re: Drawing a rectangle over Qlabel image

    Alright, but when i switched to QgraphicsView, my image is not being displayed again. Here is my code:
    MainWindow::MainWindow(Tqt_interface* tqt, QWidget *parent ) : QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    QPixmap pixMap("/home/liqi/Qt_robotino/src/image/ariccmap.png");

    QGraphicsScene scene;
    scene.addPixmap(pixMap);
    ui->graphicsViewmap->setScene(&scene);
    ui->graphicsViewmap->show();

    }

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing a rectangle over Qlabel image

    Your scene object goes out of scope.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default Re: Drawing a rectangle over Qlabel image

    Sorry but can you elaborate more on it? I don't really understand this. how do know that my scene object goes out of scope?

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing a rectangle over Qlabel image

    Because you created it on the stack as a local variable. This is plain C++ you know...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default Re: Drawing a rectangle over Qlabel image

    It might be plain c++ to you but i dont really study much of it cos all i was thought is those simple cin cout etc. this is the first time dealing with this so im here asking for help. but anyway, thanks for your replies.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing a rectangle over Qlabel image

    We can't assume here that you know nothing of programming when writing our replies, we have to assume you know how to program and you only have problems with using Qt. You need a decent knowledge of C++ to be able to use Qt efficiently. So far it seemed you didn't have problems with programming as such so it was safe to assume you just didn't notice you were using a local variable or that you didn't know it mattered.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Oct 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4

    Default Re: Drawing a rectangle over Qlabel image

    im able to draw over the image now but how do i put it to keep refreshing and change its position ?? I was thinking of putting while loop at first but it will just keep looping the same thing. any idea?

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing a rectangle over Qlabel image

    Use QGraphicsItem::setPos() to modify the position of an item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Drawing over an image displayed on a QLabel
    By franco.amato in forum Qt Programming
    Replies: 10
    Last Post: 25th August 2010, 05:40
  2. Prevent QStyledItemDelegate from drawing focus rectangle
    By stefanadelbert in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2010, 09:43
  3. problem in drawing image in QLabel!
    By mismael85 in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2010, 15:16
  4. Drawing a selection rectangle
    By Caius Aérobus in forum Qt Programming
    Replies: 7
    Last Post: 4th April 2009, 15:47
  5. Replies: 3
    Last Post: 4th June 2007, 09:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.