Results 1 to 7 of 7

Thread: How to draw a rectangle or line on an image

  1. #1
    Join Date
    Oct 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default How to draw a rectangle or line on an image

    I have loaded the image using QLabel but i dont know how to draw on it......
    This works fine as far as opening of images is concerned please guide how to draw rectangle,line etc. on the image


    #include <QtGui>

    #include "mainwindow.h"

    ImageViewer::ImageViewer()
    {
    imageLabel = new QLabel;
    imageLabel->setBackgroundRole(QPalette::Base);
    imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    imageLabel->setScaledContents(true);

    scrollArea = new QScrollArea;
    scrollArea->setBackgroundRole(QPalette:ark);
    scrollArea->setWidget(imageLabel);
    setCentralWidget(scrollArea);

    createActions();
    createMenus();

    setWindowTitle(tr("Image Viewer"));
    resize(500, 400);
    }



    void ImageViewer:pen()
    {
    QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open File"), QDir::currentPath());
    if (!fileName.isEmpty()) {
    QImage image(fileName);
    if (image.isNull()) {
    QMessageBox::information(this, tr("Image Viewer"),
    tr("Cannot load %1.").arg(fileName));
    return;
    }
    imageLabel->setPixmap(QPixmap::fromImage(image));
    scaleFactor = 1.0;


    fitToWindowAct->setEnabled(true);
    updateActions();

    if (!fitToWindowAct->isChecked())
    imageLabel->adjustSize();
    }
    }



    void ImageViewer::zoomIn()
    {
    scaleImage(1.25);
    }

    void ImageViewer::zoomOut()
    {
    scaleImage(0.8);
    }

    void ImageViewer::normalSize()
    {
    imageLabel->adjustSize();
    scaleFactor = 1.0;
    }

    void ImageViewer::fitToWindow()
    {
    bool fitToWindow = fitToWindowAct->isChecked();
    scrollArea->setWidgetResizable(fitToWindow);
    if (!fitToWindow) {
    normalSize();
    }
    updateActions();
    }



    void ImageViewer::createActions()
    {
    openAct = new QAction(tr("&Open..."), this);
    openAct->setShortcut(tr("Ctrl+O"));
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));



    exitAct = new QAction(tr("E&xit"), this);
    exitAct->setShortcut(tr("Ctrl+Q"));
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

    zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
    zoomInAct->setShortcut(tr("Ctrl++"));
    zoomInAct->setEnabled(false);
    connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));

    zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
    zoomOutAct->setShortcut(tr("Ctrl+-"));
    zoomOutAct->setEnabled(false);
    connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));

    normalSizeAct = new QAction(tr("&Normal Size"), this);
    normalSizeAct->setShortcut(tr("Ctrl+S"));
    normalSizeAct->setEnabled(false);
    connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));

    fitToWindowAct = new QAction(tr("&Fit to Window"), this);
    fitToWindowAct->setEnabled(false);
    fitToWindowAct->setCheckable(true);
    fitToWindowAct->setShortcut(tr("Ctrl+F"));
    connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));


    }

    void ImageViewer::createMenus()
    {
    fileMenu = new QMenu(tr("&File"), this);
    fileMenu->addAction(openAct);

    fileMenu->addSeparator();
    fileMenu->addAction(exitAct);

    viewMenu = new QMenu(tr("&View"), this);
    viewMenu->addAction(zoomInAct);
    viewMenu->addAction(zoomOutAct);
    viewMenu->addAction(normalSizeAct);
    viewMenu->addSeparator();
    viewMenu->addAction(fitToWindowAct);



    menuBar()->addMenu(fileMenu);
    menuBar()->addMenu(viewMenu);

    }

    void ImageViewer::updateActions()
    {
    zoomInAct->setEnabled(!fitToWindowAct->isChecked());
    zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
    normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
    }

    void ImageViewer::scaleImage(double factor)
    {
    Q_ASSERT(imageLabel->pixmap());
    scaleFactor *= factor;
    imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());

    adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
    adjustScrollBar(scrollArea->verticalScrollBar(), factor);

    zoomInAct->setEnabled(scaleFactor < 3.0);
    zoomOutAct->setEnabled(scaleFactor > 0.333);
    }

    void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
    {
    scrollBar->setValue(int(factor * scrollBar->value()
    + ((factor - 1) * scrollBar->pageStep()/2)));
    }






    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to draw a rectangle or line on an image

    You use QPainter for painting.

    Where you paint onto depends on what you want to do. QPainter works on any class that is a QPaintDevice:
    You can paint onto the QImage, onto the QPixmap created from the image, on a separate Image/Pixmap that you combine with the oroginal, or derive from QLabel and paint into the label after it has painted the image.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to draw a rectangle or line on an image

    I tried this
    QPainter painter(&image);
    painter.drawRect(0,0,100,100);


    but this doesn't work.since i am a total newbie in Qt, i am finding it difficult to get it done.
    It would be great if you could provide me with the source code or just edit my code.

    Thanks..

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to draw a rectangle or line on an image

    Can you show us full code where you are trying to draw on image and display it after drawing ?

  5. #5
    Join Date
    Oct 2013
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to draw a rectangle or line on an image

    Qt Code

    #include <QtGui>

    #include "mainwindow.h"

    ImageViewer::ImageViewer()
    {
    imageLabel = new QLabel;
    imageLabel->setBackgroundRole(QPalette::Base);
    imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    imageLabel->setScaledContents(true);

    scrollArea = new QScrollArea;
    scrollArea->setBackgroundRole(QPalette:ark);
    scrollArea->setWidget(imageLabel);
    setCentralWidget(scrollArea);

    createActions();
    createMenus();

    setWindowTitle(tr("Image Viewer"));
    resize(500, 400);
    }



    void ImageViewer:pen()
    {
    QString fileName = QFileDialog::getOpenFileName(this,
    tr("Open File"), QDir::currentPath());
    if (!fileName.isEmpty()) {
    QImage image(fileName);
    if (image.isNull()) {
    QMessageBox::information(this, tr("Image Viewer"),
    tr("Cannot load %1.").arg(fileName));
    return;
    }
    imageLabel->setPixmap(QPixmap::fromImage(image));
    scaleFactor = 1.0;


    fitToWindowAct->setEnabled(true);
    updateActions();

    if (!fitToWindowAct->isChecked())
    imageLabel->adjustSize();
    }
    }



    void ImageViewer::zoomIn()
    {
    scaleImage(1.25);
    }

    void ImageViewer::zoomOut()
    {
    scaleImage(0.8);
    }

    void ImageViewer::normalSize()
    {
    imageLabel->adjustSize();
    scaleFactor = 1.0;
    }

    void ImageViewer::fitToWindow()
    {
    bool fitToWindow = fitToWindowAct->isChecked();
    scrollArea->setWidgetResizable(fitToWindow);
    if (!fitToWindow) {
    normalSize();
    }
    updateActions();
    }



    void ImageViewer::createActions()
    {
    openAct = new QAction(tr("&Open..."), this);
    openAct->setShortcut(tr("Ctrl+O"));
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));



    exitAct = new QAction(tr("E&xit"), this);
    exitAct->setShortcut(tr("Ctrl+Q"));
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));

    zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
    zoomInAct->setShortcut(tr("Ctrl++"));
    zoomInAct->setEnabled(false);
    connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));

    zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
    zoomOutAct->setShortcut(tr("Ctrl+-"));
    zoomOutAct->setEnabled(false);
    connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));

    normalSizeAct = new QAction(tr("&Normal Size"), this);
    normalSizeAct->setShortcut(tr("Ctrl+S"));
    normalSizeAct->setEnabled(false);
    connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));

    fitToWindowAct = new QAction(tr("&Fit to Window"), this);
    fitToWindowAct->setEnabled(false);
    fitToWindowAct->setCheckable(true);
    fitToWindowAct->setShortcut(tr("Ctrl+F"));
    connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));


    }

    void ImageViewer::createMenus()
    {
    fileMenu = new QMenu(tr("&File"), this);
    fileMenu->addAction(openAct);

    fileMenu->addSeparator();
    fileMenu->addAction(exitAct);

    viewMenu = new QMenu(tr("&View"), this);
    viewMenu->addAction(zoomInAct);
    viewMenu->addAction(zoomOutAct);
    viewMenu->addAction(normalSizeAct);
    viewMenu->addSeparator();
    viewMenu->addAction(fitToWindowAct);



    menuBar()->addMenu(fileMenu);
    menuBar()->addMenu(viewMenu);

    }

    void ImageViewer::updateActions()
    {
    zoomInAct->setEnabled(!fitToWindowAct->isChecked());
    zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
    normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
    }

    void ImageViewer::scaleImage(double factor)
    {
    Q_ASSERT(imageLabel->pixmap());
    scaleFactor *= factor;
    imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());

    adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
    adjustScrollBar(scrollArea->verticalScrollBar(), factor);

    zoomInAct->setEnabled(scaleFactor < 3.0);
    zoomOutAct->setEnabled(scaleFactor > 0.333);
    }

    void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
    {
    scrollBar->setValue(int(factor * scrollBar->value()
    + ((factor - 1) * scrollBar->pageStep()/2)));
    }

    void ImageViewer::mousePressEvent(QMouseEvent *e)
    {
    p1.setX(e->x());
    p1.setY(e->y());
    }

    void ImageViewer::mouseReleaseEvent(QMouseEvent *e)
    {
    p2.setX(e->x());
    p2.setY(e->y());
    draw();
    }

    void ImageViewer::draw()
    {
    QPainter painter(&imageLabel);
    painter.drawRect(p1.x(),p1.y(),p2.x()-p1.x(),p2.y()-p1.y());
    }

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to draw a rectangle or line on an image

    Please use CODE tags, this is very hard to read.
    Qt Code:
    1. void ImageViewer::draw()
    2. {
    3. QPainter painter(&imageLabel);
    4. painter.drawRect(p1.x(),p1.y(),p2.x()-p1.x(),p2.y()-p1.y());
    5. }
    To copy to clipboard, switch view to plain text mode 
    Is imageLabel an instance of QLabel ?
    Think about it, you want to draw on a label or image ?

  7. #7
    Join Date
    Feb 2014
    Location
    San Jose
    Posts
    4
    Platforms
    Windows

    Default Re: How to draw a rectangle or line on an image

    Hello, is it possible to highlight a portion of an image by drawing a red line on the image?

Similar Threads

  1. Draw line on image using QPainter
    By Qt Coder in forum Qt Programming
    Replies: 29
    Last Post: 11th August 2015, 13:09
  2. Draw rectangle on QImage
    By ^NyAw^ in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2013, 13:33
  3. Replies: 2
    Last Post: 9th November 2012, 18:16
  4. Replies: 3
    Last Post: 27th August 2010, 07:00
  5. how to draw columns in QPainter's rectangle
    By jyoti in forum Qt Programming
    Replies: 1
    Last Post: 24th August 2007, 11:50

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.