我有一个 QGraphicsTextItem 作为一个 lineedit,使用
setTextInteractionFlags(Qt::TextEditorInteraction);
但是,如果用户按回车键,它将显示多行。我希望它忽略换行,该怎么做?
最佳答案
AFAIK QGraphicsTextItem 没有实现该功能。您可以通过子类化 QGraphicsTextItem 和过滤键盘事件来达到目的:
class MyGraphicsTextItem : public QGraphicsTextItem
{
// ...
protected:
virtual void keyPressEvent(QKeyEvent* e) override
{
if (e->key() != Qt::Key_Return)
{
// let parent implementation handle the event
QGraphicsTextItem::keyPressEvent(e);
}
else
{
// ignore the event and stop its propagation
e->accept();
}
}
};
关于c++ - 如何使 QGraphicsTextItem 单行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39201175/