Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPlainTextEdit::QCloseEvent



  • 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!


  • Lifetime Qt Champion

    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


  • Lifetime Qt Champion

    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 ?



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



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


  • Lifetime Qt Champion

    @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.



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


  • Lifetime Qt Champion

    @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.



  • @mrjj

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

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


  • Lifetime Qt Champion

    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



  • #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


  • Lifetime Qt Champion

    Hi,

    What does clearSubMenus look like ?



  • @SGaist

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


  • Lifetime Qt Champion

    I meant it signature.


Log in to reply