Results 1 to 17 of 17

Thread: QPaint over Qlabel ?

  1. #1
    Join Date
    May 2010
    Posts
    9
    Thanks
    3

    Unhappy QPaint over Qlabel ?

    Hi there,
    I am new to QT Programming so I realize this problem seem rather stupid. I apologize in advance.

    So basically I am trying to draw on an image using QPainter.
    I have a QLabel on a QScrollArea:

    Qt Code:
    1. imageLabel = new QLabel;
    2. scrollArea = new QScrollArea;
    3. scrollArea->setWidget(imageLabel);
    To copy to clipboard, switch view to plain text mode 

    after which I go ahead and load an image as a pixmap:

    Qt Code:
    1. imageLabel->setPixmap(QPixmap::fromImage(image));
    To copy to clipboard, switch view to plain text mode 

    Now I have tried creating a function:

    Qt Code:
    1. void Planning::paintNodes(QPaintEvent *){QPainter paintme(this);
    2. paintme.drawLine(QLineF(10.0, 80.0, 90.0, 20.0));
    3. }
    To copy to clipboard, switch view to plain text mode 

    In addition, i tried creating an action like so:
    Qt Code:
    1. drawNodesAct = new QAction(tr("Draw &Nodes"),this);drawNodesAct->setShortcut(tr("Ctrl+N"));drawNodesAct->setEnabled(false);connect(drawNodesAct, SIGNAL(triggered()), this, SLOT(paintNodes(image.rect())));
    To copy to clipboard, switch view to plain text mode 

    and I try to call paintNodes from another function.
    I am a little confused as to what I should define the parameter 'QPaintEvent *' to pass in as?
    I read up a little and it said I should pass a QRect. How would I be able to get this code to work to merely draw a line on the loaded image?

    I'm working on my senior design for electrical engineering and trying to create a GUI for our A* Planning algorithm.
    Any help will be appreciated!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPaint over Qlabel ?

    What kind of object is Planning?
    A widget, or a picture/pixmap/image?

    If it is a widget, is paintNodes being called from withint the paintEvent ?
    If not: check out this warning:

    Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QPaint over Qlabel ?

    Quote Originally Posted by eth0 View Post
    I am a little confused as to what I should define the parameter 'QPaintEvent *' to pass in as?
    Then why do you define such an argument? There is no need for it.

    EDIT: And you can't pass values in a connect statement!

  4. #4
    Join Date
    May 2010
    Posts
    9
    Thanks
    3

    Default Re: QPaint over Qlabel ?

    Hi,
    Thanks for the quick response.
    Plannin inherits QMainWindow:

    Qt Code:
    1. class Planning : public QMainWindow
    To copy to clipboard, switch view to plain text mode 

    I have changed the action so that it does not pass a parameter.
    I have also realized that to trigger the paintEvent I need to QWidget::repaint():

    Qt Code:
    1. void Planning::paintNodes()
    2. {
    3. PaintNodes=true;
    4. QWidget::repaint();
    5. }
    6.  
    7. void Planning::paintConnections()
    8. {
    9. PaintConnections=true;
    10. QWidget::repaint();
    11. }
    To copy to clipboard, switch view to plain text mode 

    My paintEvent is as follows:
    Qt Code:
    1. void Planning::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter paintme(this);
    4. paintme.drawLine(QLineF(10.0, 80.0, 90.0, 20.0));
    5. }
    To copy to clipboard, switch view to plain text mode 

    and it is defined in the header as such:
    Qt Code:
    1. protected:
    2. void paintEvent(QPaintEvent * event);
    To copy to clipboard, switch view to plain text mode 

    Now the program seems to compile but the line does not get drawn. It does not draw on the base window or even after loading of an image as a background pixmap.
    Any thoughts?

  5. #5
    Join Date
    May 2010
    Posts
    9
    Thanks
    3

    Default Re: QPaint over Qlabel ?

    Oh I forgot to mention,
    Here is the constructor for planning and the central widget is a QScrollArea:

    Qt Code:
    1. Planning::Planning()
    2. {
    3. //glWidget = new GLWidget;
    4. imageLabel = new QLabel;
    5. imageLabel->setBackgroundRole(QPalette::Base);
    6. imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    7. imageLabel->setScaledContents(true);
    8.  
    9. scrollArea = new QScrollArea;
    10. scrollArea->setBackgroundRole(QPalette::Dark);
    11. scrollArea->setWidget(imageLabel);
    12. scrollArea->setMouseTracking(true);
    13. setCentralWidget(scrollArea);
    14.  
    15. createActions();
    16. createMenus();
    17. statusBar()->showMessage(tr("Planning v1.0"));
    18.  
    19. setWindowTitle(tr("Planning"));
    20. resize(640, 480);}
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPaint over Qlabel ?

    Can you in simple words explain what you want to achieve ?

    You seem to be complicating things over simple requirements.

    Also take some time to check the Qt Demos,,am sure you will find something related to you, and will help you better understanding how painting in Qt works.
    Then only you will be able to get proper help from here,,, else you may not learn if solution is directly given to you

  7. #7
    Join Date
    May 2010
    Posts
    9
    Thanks
    3

    Default Re: QPaint over Qlabel ?

    Hi,
    Basically I am writing a gui interface to do path planning. We have a robot that autonomously navigates using gps, computer vision and basic microcontroller counts. The planning software should basically load an image of my school (be able to pan and zoom), draw points (i.e. nodes) and connections between the nodes. I am familiar with opengl, but cant seem to integrate into properly with qt4, so i figured i'd try to use qtpaint to do the same. When I load the image now I cant seem to draw on it. And trust me, I am an EE major, I have spent the better part of the last two weeks trying to figure out this triviality. This is why I need the help =(
    I am attaching an image for a better idea.

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPaint over Qlabel ?

    The painting should be done in your label or glwidget, or any other widget that is actually displaying what you want.
    Now you paint in the mainwindow and put a scrollview with label on top of it, that's why you do not see anything

  9. The following user says thank you to tbscope for this useful post:

    eth0 (17th May 2010)

  10. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPaint over Qlabel ?

    Ok fine... got the idea and you background...
    I guess using QGraphicsView will be best for your case, since zooming is very easy in that.
    Now coming to drawing points, you will need to override mousePressEvent of the view, maintain a list of the points,,, and then draw those points in the view's paintEvent or you may even add those lines as items in the scene.

    To make sense what I am talking, have a look at Qt Demos-> Graphics View -> Diagram Scene example.. you will get an idea how to draw a line and make it movable.
    Once you get to know how to draw lines... you can do something like -
    Qt Code:
    1. mousePressEvent()
    2. {
    3. startPoint = store point clicked;
    4. }
    5.  
    6. paintEvent()
    7. {
    8. draw line from startPoint to current mouse point.
    9. }
    10.  
    11. mouseDoubleClicked()
    12. {
    13. stop drawing
    14. }
    To copy to clipboard, switch view to plain text mode 
    Hope you get the idea

  11. The following user says thank you to aamer4yu for this useful post:

    eth0 (17th May 2010)

  12. #10
    Join Date
    May 2010
    Posts
    9
    Thanks
    3

    Default Re: QPaint over Qlabel ?

    Thanks for the ideas! I appreciate it.
    aamer4yu, I have implemented your mousePressEvent idea, thanks!

    tbcope, you said:
    The painting should be done in your label or glwidget
    I would like to futher this idea.
    How exactly would I pass the paint event to the label?
    Currently I have:
    Qt Code:
    1. void Planning::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter paintme(this);
    4. paintme.drawLine(QLineF(10.0, 80.0, 90.0, 20.0));
    5. }
    To copy to clipboard, switch view to plain text mode 

    I tried adding the following to the end of the paintEvent, but it doesn't seem to work:
    Qt Code:
    1. QLabel::paintEvent(event);
    To copy to clipboard, switch view to plain text mode 

    How exactly would I pass the paint operation to take place on the QLabel so that it appears on the pixmap?

  13. #11
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPaint over Qlabel ?

    Subclass a label widget (for example).
    In your subclass, handle the paintEvent, like you did for your main window. Do not draw lines on your label from within the main window paint event.
    If you want to paint on a widget, use the paint event of that exact widget.

    Create properties or get/set functions to set or get new points or lines.
    Then, whenever you set a new point or line, update or repaint that part of the label.

  14. #12
    Join Date
    May 2010
    Posts
    9
    Thanks
    3

    Default Re: QPaint over Qlabel ?

    I wish my c++ skills were better.
    This is what I understood from what you said:

    Qt Code:
    1. class Label : public QLabel
    2. {
    3. Q_OBJECT
    4.  
    5. protected:
    6. void paintEvent(QPaintEvent *);
    7. };
    8.  
    9. class Planning : public QMainWindow
    10. {
    11. Q_OBJECT
    12. public:
    13. <other non-essential code>
    14. Planning();
    15. Label *imageLabel;
    16. QScrollArea *scrollArea;
    17.  
    18. QPrinter printer;
    19. };
    20.  
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    This however results in:
    Qt Code:
    1. planning.h:20: error: invalid use of incomplete type ‘struct QLabel
    2. planning.h:13: error: forward declaration of ‘struct QLabel
    To copy to clipboard, switch view to plain text mode 

    I apologise in advance for my lack of knowledge about this matter.

  15. #13
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPaint over Qlabel ?

    There's a lot wrong I'm afraid.
    I'll write you an example, but that will take a couple of minutes.
    Nevertheless, I suggest reading a little bit about C++ and classes.
    The error you get is because you didn't include QLabel in your code.

  16. #14
    Join Date
    May 2010
    Posts
    9
    Thanks
    3

    Default Re: QPaint over Qlabel ?

    I appreciate all your help tbscope!

  17. #15
    Join Date
    May 2010
    Posts
    9
    Thanks
    3

    Default Re: QPaint over Qlabel ?

    So I added the following in the header file:
    Qt Code:
    1. #include <QLabel>
    To copy to clipboard, switch view to plain text mode 

    and changed the paintfunction in the cpp file as such:
    Qt Code:
    1. void Label::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter paintme(this);
    4. paintme.drawLine(QLineF(10.0, 80.0, 90.0, 20.0));
    5. }
    To copy to clipboard, switch view to plain text mode 

    It does compile now, but seg-faults instantly.
    An example would be greatly appreciated!
    Thanks.

  18. #16
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPaint over Qlabel ?

    Here's a quick example.
    This is just to demonstrate how you can create a custom label and paint on top of the existing content.

    I created a new standard gui project with creator.
    Then added two new classes:
    1. Planning, based on QScrollArea
    2. CustomLabel, based on QLabel

    I added the label to the scrollarea from within the Planning class itself. To me this seems logical as I guess the Planning class will tell the CustomLabel what to do.
    If not, you can add the CustomLabel to the Planning scroll area from within your main window too, if that makes it easier for you.

    mainwindow.h
    Qt Code:
    1. #include <QMainWindow>
    2.  
    3. #include "planning.h"
    4.  
    5. namespace Ui {
    6. class MainWindow;
    7. }
    8.  
    9. class MainWindow : public QMainWindow {
    10. Q_OBJECT
    11. public:
    12. MainWindow(QWidget *parent = 0);
    13. ~MainWindow();
    14.  
    15. protected:
    16. void changeEvent(QEvent *e);
    17.  
    18. private:
    19. Ui::MainWindow *ui;
    20.  
    21. Planning *myPlanning;
    22. };
    23.  
    24. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QGridLayout>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. QGridLayout *layout = new QGridLayout;
    13.  
    14. myPlanning = new Planning(this);
    15. layout->addWidget(myPlanning);
    16.  
    17. ui->centralWidget->setLayout(layout);
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete ui;
    23. }
    24.  
    25. void MainWindow::changeEvent(QEvent *e)
    26. {
    27. QMainWindow::changeEvent(e);
    28. switch (e->type()) {
    29. case QEvent::LanguageChange:
    30. ui->retranslateUi(this);
    31. break;
    32. default:
    33. break;
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    planning.h
    Qt Code:
    1. #ifndef PLANNING_H
    2. #define PLANNING_H
    3.  
    4. #include <QWidget>
    5. #include <QScrollArea>
    6.  
    7. #include "customlabel.h"
    8.  
    9. class Planning : public QScrollArea
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit Planning(QWidget *parent = 0);
    14.  
    15. signals:
    16.  
    17. public slots:
    18.  
    19. private:
    20. CustomLabel *customLabel;
    21. };
    22.  
    23. #endif // PLANNING_H
    To copy to clipboard, switch view to plain text mode 

    planning.cpp
    Qt Code:
    1. #include "planning.h"
    2. #include <QPixmap>
    3.  
    4. Planning::Planning(QWidget *parent) :
    5. QScrollArea(parent)
    6. {
    7. customLabel = new CustomLabel(this);
    8. setWidget(customLabel);
    9.  
    10. QPixmap pixmap(...); // use your pixmap here
    11. customLabel->setPixmap(pixmap);
    12. customLabel->resize(pixmap.size());
    13. }
    To copy to clipboard, switch view to plain text mode 

    customlabel.h
    Qt Code:
    1. #ifndef CUSTOMLABEL_H
    2. #define CUSTOMLABEL_H
    3.  
    4. #include <QLabel>
    5.  
    6. class CustomLabel : public QLabel
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit CustomLabel(QWidget *parent = 0);
    11.  
    12. signals:
    13.  
    14. public slots:
    15.  
    16. protected:
    17. void paintEvent(QPaintEvent *);
    18. };
    19.  
    20. #endif // CUSTOMLABEL_H
    To copy to clipboard, switch view to plain text mode 

    customlabel.cpp
    Qt Code:
    1. #include "customlabel.h"
    2. #include <QPainter>
    3. #include <QPaintEvent>
    4. #include <QPoint>
    5. #include <QPen>
    6.  
    7. CustomLabel::CustomLabel(QWidget *parent) :
    8. QLabel(parent)
    9. {
    10. }
    11.  
    12. void CustomLabel::paintEvent(QPaintEvent *event)
    13. {
    14. // Assuming you want to paint on top of the existing content of the label.
    15. // First draw the label
    16. QLabel::paintEvent(event);
    17.  
    18. QPainter painter(this);
    19. QPoint start(0,0);
    20. QPoint end(100,100);
    21. QPen pen(qRgb(255,0,0));
    22. painter.setPen(pen);
    23. painter.drawLine(start, end);
    24. }
    To copy to clipboard, switch view to plain text mode 

    Off course you want to rename the CustomLabel class to something more appropriate.
    And you want to create some setters and getters to set and get data. Maybe create a list to keep track of data when updating and painting etc...


    EDIT:
    Ohh, I used a jpg I have on my computer in the code, I edited that out.
    Last edited by tbscope; 19th May 2010 at 19:04. Reason: Mention custom pixmap

  19. The following user says thank you to tbscope for this useful post:

    eth0 (19th May 2010)

  20. #17
    Join Date
    May 2010
    Posts
    9
    Thanks
    3

    Default Re: QPaint over Qlabel ?

    Thank you soo much!
    Perfectly what I need.

Similar Threads

  1. Replies: 1
    Last Post: 29th September 2009, 19:44
  2. QPaint - Scale
    By jsmith in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 08:10
  3. QTDesigner + QPaint [QT 4.4]
    By pippofrank in forum Qt Programming
    Replies: 10
    Last Post: 13th December 2008, 17:45
  4. Speeding up QPaint
    By georgie in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2006, 02:06
  5. Adding numbers to circles in QPaint
    By therealjag in forum Qt Programming
    Replies: 1
    Last Post: 12th February 2006, 10:21

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.