Qt QPlainTextEdit is crashing

Qt QPlainTextEdit is crashing

本文关键字:crashing is QPlainTextEdit Qt      更新时间:2023-10-16

我正在使用一个记录器窗口,这是一个从QPlainTextEdit继承的简单小部件。

现在我需要同时打印几条消息(我用互斥锁使这个线程安全),但它还是崩溃了。这是来自 gdb 的消息

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff56c5cb9 in QTextLine::draw(QPainter*, QPointF const&, QTextLayout::FormatRange const*) const ()
from /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5

我正在使用Qt 5.4,但尝试了Qt 5.7,崩溃仍然存在。有人有提示吗?我应该从其他小部件继承吗?

正如人们在上面评论的那样,问题可能在于你做事的方式。因此,以下是如何处理您的情况的推荐方法:使用 QThread,不要使用互斥锁。

Qt使用信号/插槽进行线程安全通信。有关如何快速入门的示例:

class A : public QObject
{
    Q_OBJECT
    // ...
public slots:
    void run();
}

任何你想从另一个新线程中运行的类都需要一个"run"槽,你可以随心所欲地称呼它,但它是你在下面的实现中启动它后在新线程中调用的第一个函数:

// Create and start the thread
QThread *t = new QThread;
t->start();
// Create an object of your class and move it to the thread
A* a = new A();
a->moveToThread(t);
// Now actually run it in the thread through the signal slot system
QMetaObject::invokeMethod(a, "run", Qt::QueuedConnection);

我希望这能说清楚,如果你有任何问题,请告诉我。