Results 1 to 10 of 10

Thread: Draw Line :confused:

  1. #1
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Draw Line :confused:

    Hi,
    I am new in QT. I do some small programes in QT.4.2.2. Now I try to
    create a small program, which one draw a line in a particular widget
    ( label ). I create a form in QT4.2.2 and place three labels in that form named as Label1,
    Label2 and Label3 and a pushButton.I need to run the program like this,
    when i click on the pushbutton, then a line draw in a label3. How can i
    do this?. I create two file like this, Please help me,
    Sabeesh C.S


    testdraw.h
    ************************
    Qt Code:
    1. #ifndef MYQTAPP_H
    2. #define MYQTAPP_H
    3. #include <QWidget>
    4. #include "ui_testdraw.h"
    5.  
    6. class myQtApp : public QMainWindow, private Ui::MainWindow
    7. {
    8. Q_OBJECT
    9. public:
    10. myQtApp(QWidget *parent = 0);
    11. public slots:
    12. void draw();
    13. protected:
    14. };
    15. #endif
    To copy to clipboard, switch view to plain text mode 

    --------------------------------------------
    testdraw.cpp
    *************************
    Qt Code:
    1. #include <QtGui>
    2. #include <qpainter.h>
    3. #include "testdraw.h"
    4. myQtApp::myQtApp(QWidget *parent)
    5. {
    6. setupUi(this);
    7. connect( pushButton, SIGNAL( clicked() ), this, SLOT( draw() ) );
    8. }
    9.  
    10.  
    11. void QLabel::paintEvent( QPaintEvent * ) {
    12. QPainter painter( this );
    13. painter.setPen(Qt::blue);
    14. painter.setFont(QFont("Arial", 40));
    15. QLineF line(10.0, 10.0, 10.0, 120.0);
    16. painter.drawLine(line);
    17. }
    18.  
    19.  
    20. void myQtApp::draw() {
    21. qDebug("Hello");
    22. }
    To copy to clipboard, switch view to plain text mode 
    ------------------------------------
    Last edited by jacek; 24th July 2007 at 01:24. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw Line :confused:

    If you want to draw on a label, you have to create a subclass and use that subclass instead.

  3. #3
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: Draw Line :confused:

    How can we do this subclassing? Can you give me an instance and example?

  4. #4
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Draw Line :confused:

    Quote Originally Posted by anafor2004 View Post
    How can we do this subclassing? Can you give me an instance and example?
    Qt Code:
    1. #ifndef MYLABEL_H
    2. #define MYLABEL_H
    3. #include <QLabel>
    4.  
    5. class MyLabel : public QLabel
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. MyLabel(QWidget *parent = 0);
    11.  
    12.  
    13. };
    14. #endif
    To copy to clipboard, switch view to plain text mode 
    This is a prototype for your QLabel. Then you reimplement & add some functions according to your need.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

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

    anafor2004 (24th January 2008)

  6. #5
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: Draw Line :confused:

    Thanks for your reply but i wonder that how can we connect this paint event with my label on my form ,

    Do I have to use this?

    class drawLabel : public QLabel,public Ui::Label

  7. #6
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Draw Line :confused:

    Quote Originally Posted by anafor2004 View Post
    Thanks for your reply but i wonder that how can we connect this paint event with my label on my form ,

    Do I have to use this?

    class drawLabel : public QLabel,public Ui::Label
    No, Because It cause a problem of inheriting two QObject. you can put
    Qt Code:
    1. virtual void paintEvent ( QPaintEvent * event )
    To copy to clipboard, switch view to plain text mode 
    in your MyLabel class definition and then reimplement It in corresponding .cpp file.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  8. #7
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: Draw Line :confused:

    This is what I did, but it didnt work.Why? Can you check it ?

    cizim.h
    Qt Code:
    1. #ifndef CIZIM_H
    2. #define CIZIM_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QPainter>
    6. #include "ui_cizim.h"
    7.  
    8. class cizim : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. cizim(QWidget *parent = 0);
    14. ~cizim();
    15.  
    16. private:
    17. Ui::cizimClass ui;
    18.  
    19. protected:
    20.  
    21.  
    22. public slots:
    23. void draw();
    24.  
    25. };
    26.  
    27. #endif // CIZIM_H
    To copy to clipboard, switch view to plain text mode 


    cizim.cpp
    Qt Code:
    1. #include "cizim.h"
    2.  
    3. cizim::cizim(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. ui.setupUi(this);
    7. QObject::connect( ui.pushButton, SIGNAL( clicked() ), ui.label, SLOT( draw() ) );
    8.  
    9.  
    10. }
    11.  
    12. cizim::~cizim()
    13. {
    14.  
    15. }
    16.  
    17. void cizim::draw()
    18. {
    19. QPainter painter(ui.label);
    20. painter.setPen(Qt::blue);
    21. painter.drawLine(0,0,200,200);
    22. }
    To copy to clipboard, switch view to plain text mode 

    ui_cizim.h
    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading ui file 'cizim.ui'
    3. **
    4. ** Created: Thu 24. Jan 11:34:02 2008
    5. ** by: Qt User Interface Compiler version 4.3.3
    6. **
    7. ** WARNING! All changes made in this file will be lost when recompiling ui file!
    8. ********************************************************************************/
    9.  
    10. #ifndef UI_CIZIM_H
    11. #define UI_CIZIM_H
    12.  
    13. #include <QtCore/QVariant>
    14. #include <QtGui/QAction>
    15. #include <QtGui/QApplication>
    16. #include <QtGui/QButtonGroup>
    17. #include <QtGui/QLabel>
    18. #include <QtGui/QMainWindow>
    19. #include <QtGui/QMenuBar>
    20. #include <QtGui/QPushButton>
    21. #include <QtGui/QStatusBar>
    22. #include <QtGui/QWidget>
    23.  
    24. class Ui_cizimClass
    25. {
    26. public:
    27. QWidget *centralwidget;
    28. QPushButton *pushButton;
    29. QLabel *label;
    30. QMenuBar *menubar;
    31. QStatusBar *statusbar;
    32.  
    33. void setupUi(QMainWindow *cizimClass)
    34. {
    35. if (cizimClass->objectName().isEmpty())
    36. cizimClass->setObjectName(QString::fromUtf8("cizimClass"));
    37. cizimClass->resize(800, 600);
    38. centralwidget = new QWidget(cizimClass);
    39. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    40. pushButton = new QPushButton(centralwidget);
    41. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    42. pushButton->setGeometry(QRect(90, 180, 121, 71));
    43. label = new QLabel(centralwidget);
    44. label->setObjectName(QString::fromUtf8("label"));
    45. label->setGeometry(QRect(270, 70, 351, 331));
    46. cizimClass->setCentralWidget(centralwidget);
    47. menubar = new QMenuBar(cizimClass);
    48. menubar->setObjectName(QString::fromUtf8("menubar"));
    49. menubar->setGeometry(QRect(0, 0, 800, 19));
    50. cizimClass->setMenuBar(menubar);
    51. statusbar = new QStatusBar(cizimClass);
    52. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    53. cizimClass->setStatusBar(statusbar);
    54.  
    55. retranslateUi(cizimClass);
    56.  
    57. QMetaObject::connectSlotsByName(cizimClass);
    58. } // setupUi
    59.  
    60. void retranslateUi(QMainWindow *cizimClass)
    61. {
    62. cizimClass->setWindowTitle(QApplication::translate("cizimClass", "MainWindow", 0, QApplication::UnicodeUTF8));
    63. pushButton->setText(QApplication::translate("cizimClass", "PushButton", 0, QApplication::UnicodeUTF8));
    64. label->setText(QString());
    65. Q_UNUSED(cizimClass);
    66. } // retranslateUi
    67.  
    68. };
    69.  
    70. namespace Ui {
    71. class cizimClass: public Ui_cizimClass {};
    72. } // namespace Ui
    73.  
    74. #endif // UI_CIZIM_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 24th January 2008 at 10:45. Reason: changed [qtclass] to [code]

  9. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Draw Line :confused:

    Quote Originally Posted by anafor2004 View Post
    Qt Code:
    1. void cizim::draw()
    2. {
    3. QPainter painter(ui.label);
    4. painter.setPen(Qt::blue);
    5. painter.drawLine(0,0,200,200);
    6. }
    To copy to clipboard, switch view to plain text mode 
    You can't paint on widgets like this in Qt 4. Every widget paints itself in paintEvent(). No widget can paint on other widget and all painting happens during paintEvent().
    J-P Nurmi

  10. #9
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Draw Line :confused:

    First you implement the class like this;
    //mylabel.h
    Qt Code:
    1. #ifndef MYLABEL_H
    2. #define MYLABEL_H
    3. #include <QLabel>
    4. class MyLabel : public QLabel
    5. {
    6. Q_OBJECT
    7. public:
    8. MyLabel(QWidget *parent = 0);
    9. virtual void paintEvent ( QPaintEvent * event );
    10. };
    11. #endif
    To copy to clipboard, switch view to plain text mode 
    and then write its corresponding mylabel.cpp. and its paintEvent you draw the line. After that you can use this label class in your constructor after setupUi(this). or use
    Qt Code:
    1. #include "mylabel.h"
    2. class Ui_cizimClass
    3. {
    4. public:
    5. ....
    6. ....
    7. MyLabel *label;//QLabel *label;
    8. ...
    9. ...
    To copy to clipboard, switch view to plain text mode 
    You can attach code by the attachments tool.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  11. #10
    Join Date
    Jan 2008
    Posts
    91
    Thanks
    8

    Default Re: Draw Line :confused:

    Hi I tried to do this but i think , still make some mistake can you check please. Sorry for that I am a new beginner!!!

    ui_cizim.h (I changed QLabel as MyLabel)
    Qt Code:
    1. #include <QtGui/QButtonGroup>
    2. #include "MyLabel.h"
    3. #include <QtGui/QMainWindow>
    4. #include <QtGui/QPushButton>
    5. #include <QtGui/QStatusBar>
    6. #include <QtGui/QWidget>
    7.  
    8. class Ui_cizimClass
    9. {
    10. public:
    11. QWidget *centralwidget;
    12. QPushButton *pushButton;
    13. MyLabel *label;
    14. QStatusBar *statusbar;
    15.  
    16. void setupUi(QMainWindow *cizimClass)
    17. {
    18. if (cizimClass->objectName().isEmpty())
    19. cizimClass->setObjectName(QString::fromUtf8("cizimClass"));
    20. cizimClass->resize(800, 600);
    21. centralwidget = new QWidget(cizimClass);
    22. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    23. pushButton = new QPushButton(centralwidget);
    24. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    25. pushButton->setGeometry(QRect(90, 180, 121, 71));
    26. label = new MyLabel(centralwidget);
    27. label->setObjectName(QString::fromUtf8("label"));
    28. label->setGeometry(QRect(270, 70, 401, 331));
    29. cizimClass->setCentralWidget(centralwidget);
    30. statusbar = new QStatusBar(cizimClass);
    31. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    32. cizimClass->setStatusBar(statusbar);
    33.  
    34. retranslateUi(cizimClass);
    35.  
    36. QMetaObject::connectSlotsByName(cizimClass);
    37. } // setupUi
    38.  
    39. void retranslateUi(QMainWindow *cizimClass)
    40. {
    41. cizimClass->setWindowTitle(QApplication::translate("cizimClass", "MainWindow", 0, QApplication::UnicodeUTF8));
    42. pushButton->setText(QApplication::translate("cizimClass", "PushButton", 0, QApplication::UnicodeUTF8));
    43. label->setText(QString());
    44. Q_UNUSED(cizimClass);
    45. } // retranslateUi
    46.  
    47. };
    48.  
    49. namespace Ui {
    50. class cizimClass: public Ui_cizimClass {};
    51. } // namespace Ui
    52.  
    53. #endif // UI_CIZIM_H
    To copy to clipboard, switch view to plain text mode 

    MyLabel.h
    Qt Code:
    1. #ifndef MYLABEL_H
    2. #define MYLABEL_H
    3. #include <QLabel>
    4. #include<QPainter>
    5.  
    6. class MyLabel : public QLabel
    7. {
    8. Q_OBJECT
    9. public:
    10. MyLabel(QWidget *);
    11. virtual void paintEvent ( QPaintEvent * event )
    12. {
    13. QPainter painter(this);
    14. painter.setPen(Qt::blue);
    15. painter.drawLine(0,0,100,100);
    16.  
    17.  
    18.  
    19. }
    20. };
    21. #endif /*SON_H_*/
    To copy to clipboard, switch view to plain text mode 

    MyLabel.cpp
    Qt Code:
    1. #include "MyLabel.h"
    2. MyLabel::MyLabel(QWidget *)
    3. {
    4.  
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 


    Cizim.h
    Qt Code:
    1. #ifndef CIZIM_H
    2. #define CIZIM_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QPainter>
    6. #include "ui_cizim.h"
    7. //#include "son.h"
    8. class cizim : public QMainWindow,public QLabel
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. cizim(QWidget *parent = 0);
    14. ~cizim();
    15.  
    16. private:
    17. Ui::cizimClass ui;
    18.  
    19. protected:
    20.  
    21.  
    22. public slots:
    23. void draw();
    24.  
    25. };
    26.  
    27. #endif // CIZIM_H
    To copy to clipboard, switch view to plain text mode 

    Cizim.cpp
    Qt Code:
    1. #include "cizim.h"
    2. cizim::cizim(QWidget *parent)
    3. : QMainWindow(parent)
    4. {
    5. ui.setupUi(this);
    6. connect( ui.pushButton, SIGNAL( clicked() ),this,SLOT( draw() ) );
    7. }
    8.  
    9. cizim::~cizim()
    10. {
    11.  
    12. }
    13.  
    14. void cizim::draw()
    15. {
    16.  
    17. ui.label->repaint();
    18. //this->close();
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "cizim.h"
    2. #include "MyLabel.h"
    3. #include <QtGui>
    4. #include <QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. cizim w;
    10. w.show();
    11. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 


    Thank you for your interest.
    Last edited by anafor2004; 24th January 2008 at 11:49.

Similar Threads

  1. QTextStream : Remove a Line?
    By kaydknight in forum Qt Programming
    Replies: 7
    Last Post: 31st January 2011, 18:15
  2. Draw a line in a Label
    By sabeesh in forum Qt Programming
    Replies: 4
    Last Post: 23rd July 2007, 13:27
  3. Draw rubberband line
    By Pang in forum Qt Programming
    Replies: 4
    Last Post: 13th July 2007, 23:09
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.