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

Thread: Subclass Problem

  1. #1
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Subclass Problem

    hi, i am trying to subclass QMouseEvent into myclass.

    I got some error while compiling the program over here,

    Link::Link(QWidget *parent) :
    QMainWindow(parent), // if i add QMouseEvent here it is showing some error
    ui(new Ui::Link)

    it is saying that no matching function for call QMouseEvent::QMouseEvent().


    Some help would be welcomed

  2. #2
    Join Date
    Sep 2010
    Location
    Germany
    Posts
    28
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Subclass Problem

    Because you do not use the correct constructors.

    Qt Code:
    1. QMouseEvent ( Type type, const QPoint & position, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers )
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QMouseEvent ( Type type, const QPoint & pos, const QPoint & globalPos, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers )
    To copy to clipboard, switch view to plain text mode 

    There is no default constructor ;-) ( and there is no constructor with a QWidget * as parent)

  3. #3
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Subclass Problem

    Fine. I was trying to make a mouse click event into a TextEdit. That is why i was trying to subclass mouseevent in my class.
    Before that i got a doubt.
    Am using the QPoint,mapFromGlobal to get the cursor position. But whenever i am clicking outside the textedit, i can get print the cursor postion(using qDebug), if i click inside the TextEdit, i dont get any postion????

    Can you tell me why?? All the thing which am trying will work?

  4. #4
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclass Problem

    I will give you a minimal working example to get you started. I started a new project added a textEdit to the main form.

    Then I added a new class (MyTextEdit) which subclasses from QTextEdit:

    header file
    Qt Code:
    1. #ifndef MYTEXTEDIT_H
    2. #define MYTEXTEDIT_H
    3.  
    4. #include <QTextEdit>
    5.  
    6. class MyTextEdit : public QTextEdit
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit MyTextEdit(QWidget *parent = 0);
    11.  
    12. void mousePressEvent(QMouseEvent *e);
    13.  
    14. signals:
    15.  
    16. public slots:
    17.  
    18. };
    19.  
    20. #endif // MYTEXTEDIT_H
    To copy to clipboard, switch view to plain text mode 

    implementation:
    Qt Code:
    1. #include "mytextedit.h"
    2. #include <QDebug>
    3. #include <QMouseEvent>
    4.  
    5. MyTextEdit::MyTextEdit(QWidget *parent) :
    6. QTextEdit(parent)
    7. {
    8. }
    9.  
    10. void MyTextEdit::mousePressEvent(QMouseEvent *e)
    11. {
    12. qDebug() << e->pos();
    13.  
    14. QTextEdit::mousePressEvent(e);
    15. }
    To copy to clipboard, switch view to plain text mode 

    I then promoted the QTextEdit on the form to a MyTextEdit (right-click -> promote to)

    And this works perfectly.

    output:
    QPoint(107,59)
    QPoint(55,91)
    QPoint(128,141)
    QPoint(164,121)
    QPoint(152,88)
    QPoint(73,62)
    QPoint(21,42)
    QPoint(8,46)
    QPoint(2,45)

    Hope this helps.

  5. #5
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Subclass Problem

    Ya. Fine , I can accept the way you are subclass the TextEdit into ur Class. But what about your QMainWindow ?? That is the Form ??


    Qt Code:
    1. class Link : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5.  
    6. public:
    7. explicit Link(QWidget *parent = 0);
    8. ~Link();
    To copy to clipboard, switch view to plain text mode 
    So i have to add another subclasss along with the Link, Isn't it?
    Last edited by high_flyer; 21st January 2011 at 12:10. Reason: code tags

  6. #6
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclass Problem

    No, you don't need a form per se. You could also just include mytextedit.h in your main procedure and then create a new MyTextEdit object and show that.

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mytextedit.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. MyTextEdit* myTextEdit = new MyTextEdit;
    9. myTextEdit->show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Stef for this useful post:

    rleojoseph (21st January 2011)

  8. #7
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Subclass Problem

    Thanks a lot stef!!

  9. #8
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Subclass Problem

    Do you know any Examples which has many subclasses????

  10. #9
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Subclass Problem

    @Stef: The way you said about subclass is working. What if happens when we need to implement the MyTextEdit class into another class.

    Its like implementing the MyTextEdit class into the newclass which contains the PushButton and a textEdit Widget in the Form.

  11. #10
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclass Problem

    From your comment what I think you are trying to achieve is a form which contains your mytextedit and a pushbutton.

    What you need to do to get this working is adding a QTextEdit widget to your form (assuming you have a MainWindow with according ui file). You then rightclick on the added QTextEdit and click in the context menu on 'Promote to...' In the promoted class field you fill in 'MyTextEdit' and in the header field it should say 'mytextedit.h'.

    Next you click 'add' and 'promote'. Now you have a form with your subclassed textedit on it. You can then add a QPushbutton to the form. And have some text setted in your textedit.

    The according code:

    main.cpp:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include "mytextedit.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10. return a.exec();
    11. }
    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. private:
    19. Ui::MainWindow *ui;
    20.  
    21. private slots:
    22.  
    23. private slots:
    24. void on_pushButton_clicked();
    25. };
    26.  
    27. #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. 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. }
    15.  
    16.  
    17. void MainWindow::on_pushButton_clicked()
    18. {
    19. ui->textEdit->setText("Hello world!");
    20. }
    To copy to clipboard, switch view to plain text mode 

    mytextedit.h:
    Qt Code:
    1. #ifndef MYTEXTEDIT_H
    2. #define MYTEXTEDIT_H
    3.  
    4. #include <QTextEdit>
    5.  
    6. class MyTextEdit : public QTextEdit
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit MyTextEdit(QWidget *parent = 0);
    11.  
    12. void mousePressEvent(QMouseEvent *e);
    13.  
    14. private:
    15. QString str;
    16.  
    17. signals:
    18.  
    19.  
    20. public slots:
    21.  
    22. };
    23.  
    24. #endif // MYTEXTEDIT_H
    To copy to clipboard, switch view to plain text mode 

    mytextedit.cpp:
    Qt Code:
    1. #include "mytextedit.h"
    2. #include <QDebug>
    3. #include <QMouseEvent>
    4.  
    5. MyTextEdit::MyTextEdit(QWidget *parent) :
    6. QTextEdit(parent)
    7. {
    8. }
    9.  
    10. void MyTextEdit::mousePressEvent(QMouseEvent *e)
    11. {
    12. qDebug() << e->pos();
    13.  
    14. QTextEdit::mousePressEvent(e);
    15. }
    To copy to clipboard, switch view to plain text mode 

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

    kosasker (3rd February 2011)

  13. #11
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Subclass Problem

    Is there any other way to inherit the MyTextEdit(which inherits QTextEdit) class to another class. Now am not using drag and drop TextEdit widget.
    Lets say
    Class A
    Class B
    Class C
    Class MyTextEdit

    Now i need to inherit the Class B,C,MyTextEdit into A, how is that possible?

  14. #12
    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 Problem

    that's called "multiple inheritance". and yes, that is possible in c++.

    Qt Code:
    1. class A : public B, public C, public MyTextEdit
    To copy to clipboard, switch view to plain text mode 

  15. #13
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Subclass Problem

    ya,i know that, but when i am trying to implement it in QT, it is showing some errors!!!!

  16. #14
    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 Problem

    Quote Originally Posted by rleojoseph View Post
    ya,i know that, but when i am trying to implement it in QT, it is showing some errors!!!!
    then you are doing something wrong.

  17. #15
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclass Problem

    Quote Originally Posted by rleojoseph View Post
    ya,i know that, but when i am trying to implement it in QT, it is showing some errors!!!!
    What does your code look like, and what errors are you receiving?

  18. #16
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Subclass Problem

    @Stef; The ideas which you have given are working well. but when i tried to make use of multiple classes, i got some bugs.
    I have classes like ,
    class text : public QMainwindow,public MyTextEdit

    The error which i am getting is on main.cpp, those errors are
    Qt Code:
    1. request for member 'show' is ambiguos
    2. candidates are :void QWidget:show()
    To copy to clipboard, switch view to plain text mode 

    The textedit widget which i am using here is not a drag and drop widget. Thats why i was trying to integrate them in the class text
    Last edited by rleojoseph; 3rd February 2011 at 14:57.

  19. #17
    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 Problem

    I guess, you don't have a "show" in your class?

  20. #18
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded

    Default Re: Subclass Problem

    nope. its there. i have created a obj for the text class and i gave .show() .

  21. #19
    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 Problem

    nope. I don't ask if you call "show()". I want to know if your class has it's own "show()"-method. maybe you can post the declaration of your class...

  22. #20
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Subclass Problem

    Both classes you inherit from are QWidgets. If your calling one of their show() methods, or attempting to provide your own version, you have to disambiguate which one you intend to call/override.

    Without seeing your code, it's impossible to say where the problem lies. Honestly, this looks like a bad solution to pretty much any problem. Perhaps you should spend some time learning basic C++ before diving straight into multiple inheritance, which brings a whole host of problems along with it, and certainly before you start using Qt.

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2010, 14:07
  2. Replies: 8
    Last Post: 12th February 2010, 03:41
  3. Problem emiting signal in QTreeWidgetItem's subclass
    By Shawn in forum Qt Programming
    Replies: 12
    Last Post: 4th September 2007, 13:08
  4. Subclass
    By merry in forum General Programming
    Replies: 2
    Last Post: 1st March 2007, 11:34
  5. problem of subclass QTableItem
    By hesummar in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2006, 10:41

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.