Drawing a line



  • Hello,
    I will explain first this time. I have 2 different questions/issues.
    First off, I want to get

    void MainWindow::paintEvent(QPaintEvent *event)
    {
        //int x = image.width(); // Draw line From left to right of image (center)
        //int y = image.height(); // Draw like from to to botton of image (center)
    
        Q_UNUSED(event);
    
        // Pass "this" pointer to painter
        QPainter painter(this);
    
        // Set Pen
        painter.setPen(Qt::black);
    
        // Draw line
        painter.drawLine(0, 0, 0, 0);
    }
    

    inside of

    void MainWindow::on_crossairs_clicked()
    {
       
    
    }
    
    

    I want this to only trigger when the button is pressed, not when the application starts.

    Secondly, I wanted to draw a line through an image that is loaded when the the load button is pressed, which does load an image.

    It needs to draw a cross, but has to perfectly fit each image so the length of the lines would change each time and position. I was not sure how to do that. You can see in my code that I tried setting up a draw line event, but again it needs to be on the image that is loaded in.

    I'm not eve sure how it would detect where the image starts and ends, and draw the cross there. Thanks in advanced.
    Here is my full code:

    Header file:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QPixmap>
    #include <QGraphicsScene>
    #include <QFileDialog>
    #include <iostream>
    #include <string>
    #include <QFileDialog>
    #include <QtGui>
    #include <QPixmap>
    #include <QGraphicsScene>
    #include <QSize>
    #include <QPainter>
    
    // Qt makes a class already when creating a project, just put everying in here...
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
      Q_OBJECT
    
    public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
    
    private slots:
      void on_load_clicked();
      void on_close_clicked();
      void on_view_clicked();
      void on_crossairs_clicked();
    
    protected:
     void paintEvent(QPaintEvent *event);
    
    private:
      Ui::MainWindow *ui;
      QPixmap image;
      QImage  *imageObject;
      QGraphicsScene *scene;
      QString imagePath;
      QString fi;
    };
    
    #endif // MAINWINDOW_H
    
    

    source file (cpp)

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    using namespace std;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_load_clicked()
    
    {
        //QString imagePath = QFileDialog::getOpenFileName();
        //Prompt user to pick file
        imagePath = QFileDialog::getOpenFileName();
        //Get file path.
        imageObject = new QImage;
        imageObject->load(imagePath);
        //Load image to scene
        image = QPixmap::fromImage(*imageObject);
        scene = new QGraphicsScene(this);
        //Display image to scene.
        scene->addPixmap(image);
        scene->setSceneRect(image.rect());
        ui->graphicsView->setScene(scene);
    }
    
    void MainWindow::on_close_clicked()
    {
        //Clear the scene.
        scene->clear();
        //Clear all labels.
        ui->graphicsView->items().clear();
        ui->name->setText(QString(("")));
        ui->path->setText(QString(("")));
        ui->size->setText(QString(("")));
        ui->depth->setText(QString(("")));
    }
    
    
    void MainWindow::on_view_clicked()
    {
        //Get name of file, file name is picked from imagepath.
        QFileInfo fi(imagePath);
        QString name = fi.fileName();
        //Get the size of image, then assign to label.
        QSize size = image.size();
        int imagedepth = image.depth();
        //Set labels.
        ui->name->setText(QString("Name: %1").arg(name));
        ui->path->setText(QString("Path: %1x").arg(imagePath));
        ui->size->setText(QString("Size: %1x%2").arg(size.width()).arg(size.height()));
        ui->depth->setText(QString("Color Depth: %1x").arg(imagedepth));
    }
    
    void MainWindow::on_crossairs_clicked()
    {
       
    
    }
    
    void MainWindow::paintEvent(QPaintEvent *event)
    {
        //int x = image.width(); // Draw line From left to right of image (center)
        //int y = image.height(); // Draw line from top to bottom of image (center)
    
        Q_UNUSED(event);
    
        // Pass "this" pointer to painter
        QPainter painter(this);
    
        // Set Pen
        painter.setPen(Qt::black);
    
        // Draw line
        painter.drawLine(0, 0, 0, 0);
    }
    
    

  • Moderators

    @CookieMaster Regarding first question: you can only paint in paintEvent. So, just set a boolean variable in on_crossairs_clicked() and check it in paintEvent. Like:

    void MainWindow::paintEvent(QPaintEvent *event)
    {
        //int x = image.width(); // Draw line From left to right of image (center)
        //int y = image.height(); // Draw like from to to botton of image (center)
    
        Q_UNUSED(event);
    
        // Pass "this" pointer to painter
        if (drawCrossairs) {
            QPainter painter(this);
            // Set Pen
            painter.setPen(Qt::black);
            // Draw line
            painter.drawLine(0, 0, 0, 0);
        }
    }
    
    void MainWindow::on_crossairs_clicked()
    {
       drawCrossairs = !drawCrossairs;
    }
    

    Why do you create imageObject on the heap? You do not even delete it - memory leak. Just create it on the stack.

    If you're using QGraphicsScene then you shouldn't use paintEvent - use methods provided by QGraphicsSceneQGraphicsScene (http://doc.qt.io/qt-5/qgraphicsscene.html#addLine-1). To get coordinates see http://doc.qt.io/qt-5/qgraphicsscene.html#addPixmap "Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0)."



  • @jsulm Thank you so much

    void MainWindow::on_crossairs_clicked()
    {
        drawCrossairs = !drawCrossairs;
        MainWindow::drawline(scene);
    }
    
    void MainWindow::drawline(QGraphicsScene *event)
    {
        Q_UNUSED(event);
    
        if (drawCrossairs)
        {
          QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
          //event->addLine(0, 0, 636/2, 266/2 , pen);
          event->addLine(0, image.height()/2, image.width(), image.height()/2 , pen);
          event->addLine(image.width()/2, 0, image.width()/2, image.height() , pen);
        }
    }
    
    

Log in to reply