Results 1 to 10 of 10

Thread: Difficulty in drawing a line

  1. #1
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Difficulty in drawing a line

    Hello., I would like to give a one-line display. But what is not shown. Please help me.

    #include <QApplication>
    #include <QtWidgets>
    int main(int argc, char* argv[])
    {
    QApplication app(argc, argv);
    QPainter* painter=new QPainter;
    painter->drawLine(100,100,200,200);
    return app.exec();
    }

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficulty in drawing a line

    Where do you want to paint?

  3. #3
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: Difficulty in drawing a line

    Quote Originally Posted by Radek View Post
    Where do you want to paint?
    For example, in QMainWindow.

  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficulty in drawing a line

    Where is the main window? QApplication is not a window. It is only a control structure of your app. Therefore, create a main window, bind the painter to a widget of the main window and draw a line.

  5. The following user says thank you to Radek for this useful post:

    rezas1000 (29th July 2014)

  6. #5
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: Difficulty in drawing a line

    Thank you very much., Please do me the code. Because I know better.

  7. #6
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficulty in drawing a line

    A long story You can try this tutorial: http://www.zetcode.com/gui/qt4/ It contains simple demonstrations of discussed matter. First, learn creating a window, then paint a line on it. There are also several good books on Qt around:

    Blanchette, Summerfield - C++ GUI Programming with Qt 4, Second Edition (suitable starting point)
    Molkentin - The Book of Qt4 (suitable starting point)
    Summerfield - Advanced Qt Programming (advanced, not a starting point)
    Ezust, Ezust - An Introduction to Design Patterns in C++ with Qt (advanced, not a starting point)

  8. The following user says thank you to Radek for this useful post:

    rezas1000 (29th July 2014)

  9. #7
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: Difficulty in drawing a line

    I thank you very much. I've copied the code on that site. Error message sent. And did not compile., I've attached a file for you. Please check your. I will be very sorry.
    Attached Files Attached Files

  10. #8
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Difficulty in drawing a line

    Which errors have you got? I downloaded the files and made a project. It compiles, runs and shows the lines. I made the following updates:

    (1) Rename "untitled.pro" to something else. "Untitled" might be a reserved file name. I renamed to "zz.pro".
    (2) Remove "widgets" from zz.pro - I have Qt 4.8, "widgets" is for Qt 5.0 and higher versions. If you have Qt 5.0 (or newer), leave "widgets" in zz.pro .

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

    rezas1000 (29th July 2014)

  12. #9
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: Difficulty in drawing a line

    Why the following program does not run properly?
    thanks.
    Qt Code:
    1. #ifndef E_H
    2. #define E_H
    3. #include <QWidget>
    4. #include <QPaintEvent>
    5. class paint:public QWidget
    6. {public:
    7. paint(QWidget* parent=0);
    8. protected:
    9. void paintevent(QPaintEvent*event);
    10. };
    11. #endif // E_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QtWidgets>
    2. #include "paint.h"
    3. paint::paint(QWidget *parent): QWidget(parent)
    4. {
    5.  
    6.  
    7. setWindowTitle("Paint");
    8. resize(500, 500);}
    9. void paint::paintevent(QPaintEvent *)
    10. {
    11. QPainter painter(this);
    12. QPen pen (Qt::black, 2, Qt::SolidLine);
    13.  
    14. painter.setPen(pen);
    15. painter.drawLine(100,100,100,100);
    16. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QtWidgets>
    2. #include "paint.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc,argv);
    6. paint pa;
    7. pa.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QT=widgets
    2. SOURCES += \
    3. main.cpp \
    4. paint.cpp
    5.  
    6. HEADERS += \
    7. paint.h
    To copy to clipboard, switch view to plain text mode 
    Untitled2.jpg

  13. #10
    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: Difficulty in drawing a line

    C++ is a case sensitive language. paintevent is not the same thing as paintEvent

    Cheers,
    _

  14. The following user says thank you to anda_skoa for this useful post:

    rezas1000 (31st August 2014)

Similar Threads

  1. QPainter: line drawing along a circle
    By Jonny174 in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2012, 05:54
  2. Drawing line on table
    By kocakden in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2008, 09:50
  3. drawing an unfixed line?
    By tommy in forum Newbie
    Replies: 7
    Last Post: 26th January 2008, 17:18
  4. Line drawing in Designer
    By Doug Broadwell in forum Newbie
    Replies: 8
    Last Post: 28th March 2007, 00:02
  5. Line drawing
    By kiranraj in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2007, 06:38

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.