Results 1 to 8 of 8

Thread: defining a widget from the ui file

  1. #1
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default defining a widget from the ui file

    I'm trying to figure out how I can use the mouseReleaseEvent of the textEdit in the simple notepad example.
    http://doc.qt.io/qt-5/gettingstartedqt.html

    I understand how to set it up for the Notepad class...
    but I can't figure out how to access <widget class="QTextEdit" name="textEdit"> from the ui file.

    The closest I have come created a second QTextEdit,
    when I tried to mimic the Notepad class.

    notepad.h

    Qt Code:
    1. #ifndef NOTEPAD_H
    2. #define NOTEPAD_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTextEdit>
    6.  
    7. namespace Ui {
    8. class Notepad;
    9. class textEdit;
    10. }
    11.  
    12. class Notepad : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Notepad(QWidget *parent = 0);
    18. ~Notepad();
    19.  
    20. private slots:
    21. void on_quitButton_clicked();
    22.  
    23. private:
    24. Ui::Notepad *ui;
    25. };
    26.  
    27. class textEdit : public QTextEdit
    28. {
    29. Q_OBJECT
    30.  
    31. public:
    32. textEdit(QWidget *parent);
    33.  
    34. private slots:
    35.  
    36. void mouseReleaseEvent(QMouseEvent *event);
    37.  
    38. };
    39.  
    40. #endif // NOTEPAD_H
    To copy to clipboard, switch view to plain text mode 

    notepad.cpp

    Qt Code:
    1. #include "notepad.h"
    2. #include "ui_notepad.h"
    3.  
    4. Notepad::Notepad(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::Notepad)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Notepad::~Notepad()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Notepad::on_quitButton_clicked()
    17. {
    18. qApp->quit();
    19.  
    20. }
    21.  
    22. textEdit::textEdit(QWidget *parent) :
    23. QTextEdit(parent){}
    24.  
    25. void textEdit::mouseReleaseEvent(QMouseEvent *event)
    26. {
    27. copy();
    28. }
    To copy to clipboard, switch view to plain text mode 


    main.cpp

    Qt Code:
    1. #include "notepad.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Notepad w;
    8. w.show();
    9. textEdit txt(&w);
    10. txt.show();
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt 5.4.1 (windows 7)
    Last edited by ravas; 23rd June 2015 at 09:14.

  2. #2
    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: defining a widget from the ui file

    if you want your own QTextEdit subclass to be instantiated, you'll need to use the "promote widget" functionality.

    Make a proper header/source implementation of your text edit class and then specify that class and its header in designer when using the promote feature.

    Cheers,
    _

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

    ravas (23rd June 2015)

  4. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: defining a widget from the ui file

    Quote Originally Posted by ravas View Post
    but I can't figure out how to access <widget class="QTextEdit" name="textEdit"> from the ui file.
    You can access it with ui->textEdit from your Notepad class.

  5. #4
    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: defining a widget from the ui file

    Quote Originally Posted by yeye_olive View Post
    You can access it with ui->textEdit from your Notepad class.
    The object, yes, but ravas wants to reimplement a virtual function of QTextEdit.

    Cheers,
    _

  6. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: defining a widget from the ui file

    Quote Originally Posted by anda_skoa View Post
    The object, yes, but ravas wants to reimplement a virtual function of QTextEdit.
    Ah, yes. There is no way around widget promotion if he is to reimplement mouseReleaseEvent(). Alternatively, and depending on what he wants to achieve, he could install an event filter on the existing widget.

  7. #6
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: defining a widget from the ui file

    Thanks. I can't seem to figure it out. I keep getting:

    Qt Code:
    1. C:\1A\2015\QT\build-QuickPad-Desktop_Qt_5_4_1_MinGW_32bit-Debug\ui_notepad.h:58: error: expected type-specifier before 'textEdit'
    2. textEdit = new textEdit(centralWidget);
    3. ^
    To copy to clipboard, switch view to plain text mode 

    textedit.h
    Qt Code:
    1. #ifndef TEXTEDIT_H
    2. #define TEXTEDIT_H
    3.  
    4. #include <QTextEdit>
    5.  
    6. namespace Ui {
    7. class textEdit;
    8. }
    9.  
    10. class textEdit : public QTextEdit
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit textEdit(QWidget *parent);
    16. ~textEdit();
    17.  
    18. private slots:
    19.  
    20. void mouseReleaseEvent(QMouseEvent *event);
    21.  
    22. private:
    23. Ui::textEdit *ui;
    24. };
    25.  
    26. #endif // TEXTEDIT_H
    To copy to clipboard, switch view to plain text mode 

    textedit.cpp

    Qt Code:
    1. #include "textedit.h"
    2.  
    3. textEdit::textEdit(QWidget *parent) :
    4. QTextEdit(parent),
    5. ui(new Ui::textEdit)
    6. {
    7. ui->setupUi(this);
    8. }
    9.  
    10. textEdit::~textEdit()
    11. {
    12. delete ui;
    13. }
    14.  
    15. void textEdit::mouseReleaseEvent(QMouseEvent *event)
    16. {
    17. copy();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ravas; 23rd June 2015 at 22:03.

  8. #7
    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: defining a widget from the ui file

    You called your class "textEdit" and you seem to have used the very same character combination as the variable/object name in designer.

    Rename either the class or the object.

    Cheers,
    _

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

    ravas (23rd June 2015)

  10. #8
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: defining a widget from the ui file

    I thought it needed to be the same.
    I got it working. Thank you!

    qtext.h

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

    qtext.cpp

    Qt Code:
    1. #include "qtext.h"
    2.  
    3. qText::qText(QWidget *parent) :
    4. QTextEdit(parent) {}
    5.  
    6. void qText::mouseReleaseEvent(QMouseEvent *event)
    7. {
    8. copy();
    9. QTextEdit::mouseReleaseEvent(event);
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 8th January 2013, 22:08
  2. Defining widget regions sensitive to mouse events?
    By kachofool in forum Qt Programming
    Replies: 1
    Last Post: 29th December 2010, 02:37
  3. defining number
    By mickey in forum General Programming
    Replies: 1
    Last Post: 1st February 2008, 07:19
  4. Replies: 6
    Last Post: 15th September 2007, 23:14
  5. Defining data format in c++
    By locus in forum General Programming
    Replies: 3
    Last Post: 13th April 2007, 20:40

Tags for this Thread

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.