Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QPlainTextEdit::QCloseEvent

    General and Desktop
    4
    14
    640
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Mehodin last edited by

      Hello,

      I'm trying to connect the QCloseEvent signal to a function of mine.

      connect(configText, &QPlainTextEdit::closeEvent, this, &Firefly::clearSubMenus);
      

      Where configText is a QPlainTextEdit

      The error im getting is:

      'QWidget::closeEvent': cannot access protected member declared in class QWidget

      How do i get around this, because this HAS to be possible.

      Thanks in advance!

      aha_1980 1 Reply Last reply Reply Quote 0
      • aha_1980
        aha_1980 Lifetime Qt Champion @Mehodin last edited by aha_1980

        Hi @Mehodin

        one way to get access to protected members is to use subclassing. There you have full access.

        Edit: closeEvent is no signal, it's a virtual function you can override. the link provides an example.

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 4
        • mrjj
          mrjj Lifetime Qt Champion last edited by

          Hi
          One note.
          Are you using configText as a window ?
          If not, im not sure closeEvent Will be triggered unless you call
          QWidget::close() yourself.
          If configText is a widget inside a form, should you not hook up to the forms
          close event ?

          1 Reply Last reply Reply Quote 2
          • M
            Mehodin last edited by

            well, I am using configText as a separate window using configText->show().
            So it's a separate window.

            1 Reply Last reply Reply Quote 0
            • M
              Mehodin last edited by

              @mrjj do you maybe know a different way to just clear() it when it closes?

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @Mehodin last edited by

                @Mehodin
                Hi
                When its a window, then close Event seems pretty appropriate if you want to perform
                some task when extra floating window is closed.

                1 Reply Last reply Reply Quote 1
                • M
                  Mehodin last edited by

                  @mrjj yeah but it's not a real QWindow, it's just a widget that's floating.

                  mrjj 1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion @Mehodin last edited by mrjj

                    @Mehodin
                    Hi
                    Well any widget, you dont give a parent, will set the Be a Window flags and
                    act like a window with border etc. So using closeEvent seems fine
                    to do some last call clean up when its closed.
                    Only downside is that you have to subclass QPlainTextEdit to have access to it.

                    But since you want to call something in Firefly , you might want to do something like

                    #ifndef MYPLAINTEXT_H
                    #define MYPLAINTEXT_H
                    
                    #include <QPlainTextEdit>
                    
                    class MyPlainText : public QPlainTextEdit
                    {
                        Q_OBJECT
                    public:
                        explicit MyPlainText(QWidget *parent = nullptr) : QPlainTextEdit(parent) {}
                    signals:
                        void Closing(); // define new signal
                    
                    protected:
                        virtual void closeEvent(QCloseEvent *event) override
                        {
                            emit Closing(); // send signal before closing.
                            QPlainTextEdit::closeEvent(event);
                        }
                    };
                    
                    #endif // MYPLAINTEXT_H
                    

                    and hook the new signal Closing to Firefly::clearSubMenus
                    from outside. That way, MyPlainText dont need any access to Firefly.

                    1 Reply Last reply Reply Quote 4
                    • M
                      Mehodin last edited by

                      @mrjj

                      0_1550599921743_d9a3530b-c89c-4b14-b500-562040496230-afbeelding.png

                      That's somehow giving me a lot of errors though when using it.

                      1 Reply Last reply Reply Quote 0
                      • mrjj
                        mrjj Lifetime Qt Champion last edited by mrjj

                        Hi
                        Hmm. compiles here.
                        How did you use it ?
                        should be placed in its own .h file and included in the project.
                        Then you hook up new signal and show the widget.

                        void MainWindow::on_pushButton_clicked()
                        {
                            auto *win = new MyPlainText();
                            win->setAttribute(Qt::WA_DeleteOnClose);
                            connect(win, &MyPlainText::Closing, this, []() {
                                qDebug() << "win is closing";
                            });
                        
                            win->show();
                        }
                        
                        

                        test project
                        https://www.dropbox.com/s/hg8xz8uour34cc8/untitled79.zip?dl=0

                        1 Reply Last reply Reply Quote 0
                        • M
                          Mehodin last edited by

                          #ifndef MYPLAINTEXT_H
                          #define MYPLAINTEXT_H

                          #include <QPlainTextEdit>

                          class textWindow : public QPlainTextEdit
                          {
                          Q_OBJECT
                          public:
                          explicit textWindow(QWidget *parent = nullptr) : QPlainTextEdit(parent) {}
                          signals:
                          void Closing(); // define new signal

                          protected:
                          virtual void closeEvent(QCloseEvent *event) override
                          {
                          emit Closing(); // send signal before closing.
                          QPlainTextEdit::closeEvent(event);
                          }
                          };
                          #endif

                          in a .h file

                          proxyText = new textWindow();
                          configText = new textWindow();
                          comboText = new textWindow();
                          connect(proxyText, &textWindow::Closing, this, &Firefly::clearSubMenus);
                          //connect(configText, &textWindow::Closing, this, &Firefly::clearSubMenus);
                          //connect(comboText, &textWindow::Closing, this, &Firefly::clearSubMenus);
                          

                          in firefly.cpp

                          @mrjj

                          1 Reply Last reply Reply Quote 0
                          • SGaist
                            SGaist Lifetime Qt Champion last edited by

                            Hi,

                            What does clearSubMenus look like ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply Reply Quote 0
                            • M
                              Mehodin last edited by

                              @SGaist

                              The 3 text edits ->clear(); (on phone right now)

                              1 Reply Last reply Reply Quote 0
                              • SGaist
                                SGaist Lifetime Qt Champion last edited by

                                I meant it signature.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply Reply Quote 1
                                • First post
                                  Last post