有没有办法在执行默认处理程序之前连接信号?我正在寻找一种方法来在QLineEdit::textChanged信号之前执行我的函数,以执行关于最大长度限制的通知。
GTK+有connect_before()、connect()和connect_after()。Qt中也有类似的东西吗?
发布于 2018-12-10 03:31:39
您可以使用keyPressEvent方法发出自定义信号。
#include <QtWidgets>
class LineEdit: public QLineEdit
{
Q_OBJECT
public:
using QLineEdit::QLineEdit;
signals:
void maxLengthSignal();
protected:
void keyPressEvent(QKeyEvent *event) override{
if(!event->text().isEmpty() && maxLength() == text().length())
emit maxLengthSignal();
QLineEdit::keyPressEvent(event);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LineEdit w;
QObject::connect(&w, &QLineEdit::textEdited, [](const QString & text){
qDebug()<< text;
});
QObject::connect(&w, &LineEdit::maxLengthSignal, [](){
qDebug()<< "maxLength signal";
});
w.setMaxLength(10);
w.show();
return a.exec();
}
#include "main.moc"https://stackoverflow.com/questions/53695621
复制相似问题