Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Promoting QLabel

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

    Default Promoting QLabel

    Hi All
    I already promoted my QLabel, and my purpose is to draw a Line on QLabel a line when I click a button but some thing wrong here I couldn't find any thing.I add my codes here . Thank you very much.

    mylabel.h
    Qt Code:
    1. #ifndef MYLABEL_H
    2. #define MYLABEL_H
    3. #include <QLabel>
    4. class myLabel :public QLabel
    5. { Q_OBJECT
    6. public:
    7. myLabel(QWidget *parent);
    8. protected:
    9. void paintEvent(QPaintEvent *pe);
    10. };
    11. #endif
    To copy to clipboard, switch view to plain text mode 

    mylabel.cpp
    Qt Code:
    1. #include <QPainter>
    2. #include "myLabel.h"
    3.  
    4. myLabel::myLabel (QWidget *parent)
    5. : QLabel(parent)
    6. {}
    7. void myLabel::paintEvent(QPaintEvent *pe)
    8. {
    9. QPainter painter(this);
    10. painter.setPen( Qt::blue );
    11. painter.drawLine(0,0,400,320);
    12. }
    To copy to clipboard, switch view to plain text mode 

    deneme.cpp
    Qt Code:
    1. #include "deneme.h"
    2. #include "myLabel.h"
    3. deneme::deneme(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. ui.setupUi(this);
    7. connect(ui.pushButton,SIGNAL(clicked()),ui.label,SLOT(drw()));
    8. }
    9. deneme::~deneme()
    10. {
    11.  
    12. }
    13. void deneme::drw()
    14. {
    15. ui.label->update();
    16. }
    To copy to clipboard, switch view to plain text mode 

    deneme.h
    Qt Code:
    1. #ifndef DENEME_H
    2. #define DENEME_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_deneme.h"
    6. class deneme : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. deneme(QWidget *parent = 0);
    12. ~deneme();
    13. private:
    14. Ui::denemeClass ui;
    15.  
    16. protected:
    17. void drw();
    18. };
    19. #endif // DENEME_H
    To copy to clipboard, switch view to plain text mode 


    somepart of ui_deneme.h
    Qt Code:
    1. #ifndef UI_DENEME_H
    2. #define UI_DENEME_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QMainWindow>
    9. #include <QtGui/QPushButton>
    10. #include <QtGui/QStatusBar>
    11. #include <QtGui/QWidget>
    12. #include "mylabel.h"
    13.  
    14. class Ui_denemeClass
    15. {
    16. public:
    17. QWidget *centralwidget;
    18. QPushButton *pushButton;
    19. myLabel *label;
    20. QStatusBar *statusbar;
    21.  
    22. void setupUi(QMainWindow *denemeClass)
    23. {
    24. if (denemeClass->objectName().isEmpty())
    25. denemeClass->setObjectName(QString::fromUtf8("denemeClass"));
    26. denemeClass->resize(800, 480);
    27. denemeClass->setMinimumSize(QSize(800, 480));
    28. denemeClass->setMaximumSize(QSize(800, 480));
    29. centralwidget = new QWidget(denemeClass);
    30. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    31. pushButton = new QPushButton(centralwidget);
    32. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    33. pushButton->setGeometry(QRect(70, 140, 75, 24));
    34. label = new myLabel(centralwidget);
    35. label->setObjectName(QString::fromUtf8("label"));
    36. label->setGeometry(QRect(150, 0, 471, 341));
    37. denemeClass->setCentralWidget(centralwidget);
    38. statusbar = new QStatusBar(denemeClass);
    39. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    40. denemeClass->setStatusBar(statusbar);
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 25th January 2008 at 07:51. Reason: changed [qtclass] to [code]

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

    Default Re: Promoting QLabel

    sorry I didn't mention about problem is it is drawing on label when start the exe. It is not waiting pushing button. I think something effecting myLabel::paintEvent but I could't find it.
    Last edited by jpn; 25th January 2008 at 07:52. Reason: Disabled smilies

  3. #3
    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: Promoting QLabel

    You could add a boolean flag to your custom label.
    Qt Code:
    1. ui.label->setDrawingEnabled(true);
    2. ui.label->update();
    To copy to clipboard, switch view to plain text mode 
    In MyLabel::paintEvent() you check the flag and draw only if enabled.
    J-P Nurmi

  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: Promoting QLabel

    Qt Code:
    1. ui.label->setDrawingEnabled(true);
    To copy to clipboard, switch view to plain text mode 
    I am unable to find setDrawingEnabled(). Dear JPN, can you tell me this is a Qt4.3.1 function or self to create. In my case error: ‘class QLabel’ has no member named ‘setDrawingEnabled’. Is this a function of other class?
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  5. #5
    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: Promoting QLabel

    Quote Originally Posted by ashukla View Post
    Qt Code:
    1. ui.label->setDrawingEnabled(true);
    To copy to clipboard, switch view to plain text mode 
    I am unable to find setDrawingEnabled(). Dear JPN, can you tell me this is a Qt4.3.1 function or self to create. In my case error: ‘class QLabel’ has no member named ‘setDrawingEnabled’. Is this a function of other class?
    I meant that he could write such function himself. All it would do is to enabled/disable the flag which is checked in paintEvent().
    J-P Nurmi

  6. #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: Promoting QLabel

    Quote Originally Posted by jpn View Post
    I meant that he could write such function himself. All it would do is to enabled/disable the flag which is checked in paintEvent().
    Yes! I have done it previously but not getting the output. Output means line inside a QLabel or QTextEdit using this way.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

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

    Default Re: Promoting QLabel

    Yes I also done it but it doesnt work!!

  8. #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: Promoting QLabel

    May we see the relevant code?
    J-P Nurmi

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

    Default Re: Promoting QLabel

    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 *);
    9. void setDrawflag(bool a);
    10.  
    11. protected:
    12. void paintEvent ( QPaintEvent * event );
    13. private:
    14. bool drawflag;
    15. };
    16. #endif
    To copy to clipboard, switch view to plain text mode 


    mylabel.cpp

    Qt Code:
    1. #include"MyLabel.h"
    2. #include <QPainter>
    3. #include <QLabel>
    4. MyLabel::MyLabel(QWidget *parent)
    5. : QLabel(parent)
    6. {
    7. }
    8. void MyLabel::paintEvent(QPaintEvent *event)
    9. {
    10.  
    11. QPainter painter(this);
    12. if(drawflag==true)
    13. {
    14. QPainter painter(this);
    15. setBackgroundRole(QPalette::Dark);
    16. setAutoFillBackground(true);
    17. painter.setPen(Qt::blue);
    18. painter.drawLine(0,0,401,331);
    19. }
    20. }
    21.  
    22. void MyLabel::setDrawflag(bool a)
    23. {
    24. drawflag=a;
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 25th January 2008 at 10:41. Reason: changed [qtclass] to [code]

  10. #10
    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: Promoting QLabel

    Could you use the #-button instead of Qt-button for code blocks, thank you. Former is for code blocks, latter is for referencing docs of a Qt class.

    So, what does "doesn't work" mean this time? Apparently you want to initialize the flag to a sane value, but I don't see why shouldn't the line get painted after you set the flag to true. But do you perhaps mean that the label should paint its content too, like text/pixmap/whatever you have set? In this case you would have to call the base class implementation of paintEvent().
    J-P Nurmi

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Promoting QLabel

    Is this some school/academic task? Why are the two of you dealing with exactly the same problem?

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

    Default Re: Promoting QLabel

    I think we even not living same country , I know there are so many new beginner in Qt and people fall with same problems.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Promoting QLabel

    Problems of drawing a line across a label? I didn't know it was that common

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

    Default Re: Promoting QLabel

    this may be very simple project, I have to work harder.

  15. #15
    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: Promoting QLabel

    Quote Originally Posted by anafor2004 View Post
    this may be very simple project, I have to work harder.
    I have getting the another way;
    label->setText("<hr>"+label->text());
    but by this you can not specify the coordinates.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Promoting QLabel

    Guys... Try this please:

    Qt Code:
    1. QFont f = label->font();
    2. f.setStrikeOut(true);
    3. label->setFont(f);
    4. label->setText("xxxxx");
    To copy to clipboard, switch view to plain text mode 
    Is that what you want?

  17. #17
    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: Promoting QLabel

    Quote Originally Posted by wysota View Post
    Guys... Try this please:

    Qt Code:
    1. QFont f = label->font();
    2. f.setStrikeOut(true);
    3. label->setFont(f);
    4. label->setText("xxxxx");
    To copy to clipboard, switch view to plain text mode 
    Is that what you want?
    Dear Sir!
    The above code StrikeOut the text of QLabel. I want to draw a line of on any widget's Background. For example I take here QLabel. But I cann't understand why line is not drwan using the paintEvent.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Promoting QLabel

    Maybe it is drawn but then the widget overdraws it with its own paint event routine.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class W : public QTextEdit {
    4. public:
    5. W() : QTextEdit(){}
    6. protected:
    7. void paintEvent(QPaintEvent *e){
    8. QTextEdit::paintEvent(e);
    9. QPainter p(viewport());
    10. QPen pe = p.pen();
    11. pe.setWidth(4);
    12. pe.setColor(Qt::red);
    13. p.setPen(pe);
    14. p.drawLine(rect().topLeft(), rect().bottomRight());
    15. }
    16. };
    17.  
    18. int main(int argc, char **argv){
    19. QApplication app(argc, argv);
    20. W w;
    21. w.show();
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  19. #19
    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: Promoting QLabel

    Qt Code:
    1. void paintEvent ( QPaintEvent * event )
    2. {
    3. QTextEdit::paintEvent(event);
    4. QPainter painter(viewport());
    5. painter.setPen(Qt::blue);
    6. painter.drawLine(rect().topLeft(),rect().bottomRight());
    7. }
    To copy to clipboard, switch view to plain text mode 
    It is working fine. I am forgotten to call viewport() and paintEvent inside paintEvent(). Thanks for your kind help.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Promoting QLabel

    You should have received a warning in the console.

Similar Threads

  1. empty pixmap as a QLabel
    By tommy in forum Qt Programming
    Replies: 16
    Last Post: 11th December 2007, 22:15
  2. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 18:57
  3. QLabel links?
    By gfunk in forum Qt Programming
    Replies: 3
    Last Post: 23rd December 2006, 01:42
  4. QScrollArea display custom QLabel
    By spawnwj in forum Qt Programming
    Replies: 6
    Last Post: 6th December 2006, 04:38
  5. QT4 layout of complex dialog is very slow
    By cboles in forum Qt Programming
    Replies: 15
    Last Post: 28th April 2006, 20:57

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.