0

如何对子类 QTextEdit 进行编码以在“输入”按键上引起“插槽”调用?

//mainwindow.cpp
myTextEdit *mytext = new myTextEdit(this);
mytext->setGeometry(QRect(QPoint(10,20), QSize(100,50)));
connect(mytext, SIGNAL(myReturn()), this, SLOT(handleText()));

    //subclassed QTextEdit
void myTextEdit::keyPressEvent(QKeyEvent *e)
{
    QString qstr = ::myTextEdit::toPlainText();

    if(e->key() == 16777220){ //enter key
        qstr = qstr + "\r\n>> ";
        //now cause an event like a QPushButton "released()" event to cause a "slot" call 
    }else{
        qstr = qstr + e->key();
    }
    ::myTextEdit::setText(qstr);
}
4

1 回答 1

0

//标题

signals:
    void OnEnter;

void myTextEdit::keyPressEvent(QKeyEvent *e)
    {
        QString qstr = ::myTextEdit::toPlainText();

        if(e->key() == 16777220){ //enter key
            qstr = qstr + "\r\n>> ";
            //now cause an event like a QPushButton "released()" event to cause a "slot" call 
            emit OnEnter(); //creates the emit signal to call a slot
        }else{
            qstr = qstr + e->key();
        }
        ::myTextEdit::setText(qstr);
    }

设置插槽,如 QPushButton

于 2013-08-01T20:51:53.507 回答