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

Thread: subclass the QLabel

  1. #1
    Join Date
    Mar 2011
    Posts
    36
    Thanks
    30
    Qt products
    Qt4
    Platforms
    Windows

    Default subclass the QLabel

    I want to get the coordinate from Qlabel when I double click on...
    how do I sub class the Qlabel in ui application
    my code looks:
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: subclass the QLabel

    is your question probably about promition if you google it? If I understand well, you want to give extra features to QLabel, than in the first step "promote to" is your friend, you may find more information in the documentation.
    Szilvi

  3. #3
    Join Date
    Mar 2011
    Posts
    36
    Thanks
    30
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: subclass the QLabel

    is your question probably about promition if you google it?
    Is your answer to this thread is promotion??

  4. #4
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: subclass the QLabel

    hi , u can override the mousPressEvent
    Qt Code:
    1. void QLabel::mousePressEvent(QMouseEvent *evt)
    2. {
    3. qDebug()<<"X"<<evt->x()<<"Y"<<evt->y();
    4. }
    To copy to clipboard, switch view to plain text mode 

    hope it helps
    Bala

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

    ready (25th March 2011)

  6. #5
    Join Date
    Mar 2011
    Posts
    36
    Thanks
    30
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: subclass the QLabel

    thanks bala...
    but i am getting confused where should i declare the class to override the mousepressevent..
    is it in mainwindows.h??

  7. #6
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: subclass the QLabel

    put it in mainwindow.cpp

    hope it helps
    Bala

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

    ready (25th March 2011)

  9. #7
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: subclass the QLabel

    Quote Originally Posted by BalaQT View Post
    hi , u can override the mousPressEvent
    Qt Code:
    1. void QLabel::mousePressEvent(QMouseEvent *evt)
    2. {
    3. qDebug()<<"X"<<evt->x()<<"Y"<<evt->y();
    4. }
    To copy to clipboard, switch view to plain text mode 

    hope it helps
    Bala
    Do you really think this will compile and link? No comment!


    @topic:
    The simples way to solve this problem is to install event filter see: QObject::installEventFilter for details.

    Another think you are doing common mistake in asking questions!
    You giving us ready solution of your problem and asking how to make it work.
    First you should explain what you want to achieve then explain how you tried to solve it this problem. I wouldn't be surprised if some would give you a better solution of primary problem.
    Last edited by MarekR22; 25th March 2011 at 09:55.

  10. The following user says thank you to MarekR22 for this useful post:

    ready (25th March 2011)

  11. #8
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: subclass the QLabel

    Do you really think this will compile and link? No comment!
    yes it is compiling. whats wrong?
    have you try in your end?

    bala

  12. The following user says thank you to BalaQT for this useful post:

    ready (25th March 2011)

  13. #9
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: subclass the QLabel

    does it work?

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

    ready (25th March 2011)

  15. #10
    Join Date
    Mar 2011
    Posts
    36
    Thanks
    30
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: subclass the QLabel

    thanks marek
    the event filter class u mentioned above...
    do I need to declare this class in mainwindow.cpp(of ui class)

  16. #11
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: subclass the QLabel

    I still think that your approach is totally wrong! QLabel is design just to visualize text not for user interaction. What you are try to do in first place?

  17. The following user says thank you to MarekR22 for this useful post:

    ready (25th March 2011)

  18. #12
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: subclass the QLabel

    @MarekR22
    does it work?
    yes its working fine , what is wrong?
    eventfilter is good too. just i gave a quick reply.

    @ready
    as marek22 says, what you want to acheive , can u explain?
    Bala


    Added after 14 minutes:


    do I need to declare this class in mainwindow.cpp(of ui class)
    this is not class. this is eventfilter. put it in the mainwindow.cpp
    bala
    Last edited by BalaQT; 25th March 2011 at 10:36.

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

    ready (25th March 2011)

  20. #13
    Join Date
    Mar 2011
    Posts
    36
    Thanks
    30
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: subclass the QLabel

    how to install eventfilter for getting mouse coordinate from Qlabel.........please provide me detail

  21. #14
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: subclass the QLabel

    hi,
    how to install eventfilter for getting mouse coordinate from Qlabel.........please provide me detail
    Qt Code:
    1. bool MainWindow::eventFilter(QObject *pObject, QEvent *evt)
    2. {
    3. if(pEv->type()==QEvent::MouseButtonDblClick) /* mouse button is doublclicked*/
    4. {
    5. QLabel *lb=qobject_cast<QLabel *>(pObject);
    6. if(lb)
    7. {
    8. QMouseEvent *mEvt=static_cast<QMouseEvent *>(ect);
    9. qDebug()<<"dbl clicked"<<evt->x()<<","<<evt->y();
    10. }
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    and use installEventFilter(obj) to install this event filter
    ex:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow w;
    5. w.show();
    6. a.installEventFilter(&w);
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    hope it helps
    bala
    Last edited by BalaQT; 25th March 2011 at 11:34.

  22. The following user says thank you to BalaQT for this useful post:

    ready (25th March 2011)

  23. #15
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: subclass the QLabel

    and don't forget
    Qt Code:
    1. label->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    edit: oh, maybe I should read posts until the end...
    edit2: ah, the information was added by edit

  24. The following user says thank you to FelixB for this useful post:

    ready (25th March 2011)

  25. #16
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: subclass the QLabel

    @felix:
    ha, u and me are posting at the same time,im at 16:4 u r at 16:3
    urz is better felix label->installEventFilter(this);

    bala

  26. The following user says thank you to BalaQT for this useful post:

    ready (25th March 2011)

  27. #17
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: subclass the QLabel

    Qt Code:
    1. // to install main window event filter for label:
    2. MainWidow::MainWidow ....
    3. {
    4. .....
    5. ui->someLabel->installEventFilter(this);
    6. .....
    7. }
    8.  
    9. bool MainWidow::eventFilter(QObject *obj, QEvent *event)
    10. {
    11. if (event->type() == QEvent::MouseButtonDblClick && obj == ui->someLabel) {
    12. QMouseEvent* moseEvent = statc_cast<QMouseEvent*>(event);
    13. QPoint position = moseEvent->pos();
    14. // do something
    15. }
    16. return QMainWidow::eventFilter(obj, event);
    17. }
    To copy to clipboard, switch view to plain text mode 

    Still I'm sure this is bad solution for the real problem you hiding from us .

  28. The following user says thank you to MarekR22 for this useful post:

    ready (25th March 2011)

  29. #18
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: subclass the QLabel

    I'd rather use dynamic_cast
    Qt Code:
    1. QMouseEvent *mEvt=dynamic_cast<QMouseEvent *>(ect);
    To copy to clipboard, switch view to plain text mode 
    static_cast does not provide runtime checks for casts, so you can create faulty code like this one:
    Qt Code:
    1. #include <iostream>
    2. #include <vector>
    3. #include <string>
    4.  
    5. class A{
    6. public:
    7. A(){
    8. }
    9. virtual ~A(){
    10. }
    11. };
    12.  
    13. class B : public A{
    14. public:
    15. B(){
    16. // add some data to this object, note that there is no such data in objects of class A
    17. for( int x=0 ; x<1000 ; ++x ){
    18. v.push_back(x);
    19. }
    20. s = "a string data";
    21. }
    22. void thisMethodIsNotInA(){
    23. std::cout << "method from B: "<<s << v.size() << " " <<"\n";
    24. }
    25. std::string s;
    26. std::vector<int> v;
    27. };
    28.  
    29. int main(){
    30. A * a = new A(); // a is a pointer to A
    31. B * b = dynamic_cast<B*>(a); // this will return NULL, because a is not pointer to B
    32. if( b ){
    33. std::cout << "dynamic cast ok ?!\n"; // you should not see this line
    34. }
    35. B * b_static = static_cast<B*>(a);
    36. if( b_static ){
    37. std::cout << "watch out for static_cast !\n"; // probably you'll see this line ...
    38. b_static->thisMethodIsNotInA(); // ... but not this one ;)
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 
    dynamic_cast will check that a is not a B*, so b == NULL, while static_cast will return non-zero pointer and calling the method from B will result in a crash.
    I know that it should work in case of QEvents, but up- and down- class casting should be done with dynamic_cast IMHO.

    ---
    edit: tested with recent version of g++ btw.
    Last edited by stampede; 25th March 2011 at 12:04. Reason: typo and additional comment

  30. The following user says thank you to stampede for this useful post:

    ready (25th March 2011)

  31. #19
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: subclass the QLabel

    Quote Originally Posted by stampede View Post
    I'd rather use dynamic_cast
    dynamic_cast will check that a is not a B*, so b == NULL, while static_cast will return non-zero pointer and calling the method from B will result in a crash.
    I know that it should work in case of QEvents, but up- and down- class casting should be done with dynamic_cast IMHO.

    ---
    edit: tested with recent version of g++ btw.
    In this case dynamic_cast is not needed overhead, "if(pEv->type()==QEvent::MouseButtonDblClick) " statement gives you warranty that ect is pointer to QMouseEvent.

  32. The following user says thank you to MarekR22 for this useful post:

    ready (25th March 2011)

  33. #20
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: subclass the QLabel

    In this case dynamic_cast is not needed overhead, "if(pEv->type()==QEvent::MouseButtonDblClick) " statement gives you warranty that ect is pointer to QMouseEvent
    I really doubt that you can feel this overhead in case of mouse press events ( probably depends on how fast you click:P ).
    Ok, probably I should write:
    up- and down- class casting should be done with dynamic_cast ...
    ... as long as it won't hurt performance

  34. The following user says thank you to stampede for this useful post:

    ready (25th March 2011)

Similar Threads

  1. Replies: 8
    Last Post: 12th February 2010, 03:41
  2. Replies: 1
    Last Post: 29th September 2009, 20:44
  3. Replies: 1
    Last Post: 2nd August 2008, 16:46
  4. Subclass Pushbutton
    By Smith in forum Newbie
    Replies: 6
    Last Post: 8th December 2007, 13:56
  5. Subclass
    By merry in forum General Programming
    Replies: 2
    Last Post: 1st March 2007, 11:34

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.