Results 1 to 8 of 8

Thread: defining a widget from the ui file

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 08:14.

Similar Threads

  1. Replies: 4
    Last Post: 8th January 2013, 21:08
  2. Defining widget regions sensitive to mouse events?
    By kachofool in forum Qt Programming
    Replies: 1
    Last Post: 29th December 2010, 01:37
  3. defining number
    By mickey in forum General Programming
    Replies: 1
    Last Post: 1st February 2008, 06:19
  4. Replies: 6
    Last Post: 15th September 2007, 22:14
  5. Defining data format in c++
    By locus in forum General Programming
    Replies: 3
    Last Post: 13th April 2007, 19: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.