【问题标题】:QApplication method setStyleSheet called from other thread causes segmentation fault从其他线程调用的 QApplication 方法 setStyleSheet 导致分段错误
【发布时间】:2016-06-02 17:03:55
【问题描述】:

问题:

分段错误的原因可能是什么?

或

如何更改背景图片。

我写道:

static QApplication *_app;

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    _app = &app;
    ...
    app.setStyleSheet(ReadStyleSheet(STYLE_SHEET_WITH_BLUE_SCREEN));
    ...
}

// called with STYLE_SHEET_WITH_BLUE_SCREEN
void qtobeapp_setStyleSheet(int typeOfSheet)
{
    syslog(LOG_DEBUG, "main: qtobeapp_setStyleSheet typeOfSheet[%d] _app[%p] 5", typeOfSheet, _app);
    _app->setStyleSheet( ReadStyleSheet(typeOfSheet) );
    syslog(LOG_DEBUG, "main: qtobeapp_setStyleSheet typeOfSheet[%d] _app[%p] 5", typeOfSheet, _app);
}

当我调用 qtobeapp_setStyleSheet(STYLE_SHEET_WITH_BLUE_SCREEN);来自不同的线程

我会得到:

Jun  2 17:37:30 Viper user.debug QT_OBEAPP: main: qtobeapp_setStyleSheet typeOfSheet[1] _app[0xbffceab4] 5
Jun  2 17:37:30 Viper user.debug QT_OBEAPP: signal_handler: SIGNAL [11] caught. Sending PID: 0, UID: 11
Jun  2 17:37:30 Viper user.debug QT_OBEAPP: main: qtobeapp_setStyleSheet typeOfSheet[1] _app[0xbffceab4] 5
Jun  2 17:37:32 Viper user.err QT_OBEAPP: signal_handler: SIGNAL [11] caught. Stack trace size: 24
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /afc/obe/QT_OBEAPP [0x1002697c]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: [0x100374]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4 [0xea97498]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4 [0xea99060]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4 [0xea9d6a0]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4(_ZNK14QWidgetPrivate15paintBackgroundEP8QPainterRK7QRegioni+0x1a0) [0xe711808]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4(_ZN14QWidgetPrivate10drawWidgetEP12QPaintDeviceRK7QRegionRK6QPointiP8QPainterP19QWidgetBackingStore+0x3f0) [0xe71bb0c]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4 [0xe902a48]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4(_ZN14QWidgetPrivate16syncBackingStoreEv+0xa8) [0xe70c100]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4(_ZN7QWidget5eventEP6QEvent+0x7f4) [0xe71d7d4]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4(_ZN11QMainWindow5eventEP6QEvent+0x100) [0xeb78028]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4(_ZN19QApplicationPrivate13notify_helperEP7QObjectP6QEvent+0xe4) [0xe6b8578]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4(_ZN12QApplication6notifyEP7QObjectP6QEvent+0x26c) [0xe6bf8cc]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtCore.so.4(_ZN16QCoreApplication14notifyInternalEP7QObjectP6QEvent+0xa8) [0xe20f4c8]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtCore.so.4(_ZN23QCoreApplicationPrivate16sendPostedEventsEP7QObjectiP11QThreadData+0x348) [0xe210ab0]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtCore.so.4(_ZN16QCoreApplication16sendPostedEventsEP7QObjecti+0x34) [0xe210c7c]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4 [0xe74c74c]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtCore.so.4(_ZN10QEventLoop13processEventsE6QFlagsINS_17ProcessEventsFlagEE+0x60) [0xe20e34c]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtCore.so.4(_ZN10QEventLoop4execE6QFlagsINS_17ProcessEventsFlagEE+0xdc) [0xe20e568]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtCore.so.4(_ZN16QCoreApplication4execEv+0xe4) [0xe210d80]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libQtGui.so.4(_ZN12QApplication4execEv+0x30) [0xe6b7924]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /afc/obe/QT_OBEAPP(main+0x380) [0x100267a8]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libc.so.6 [0xdf076a0]
Jun  2 17:37:32 Viper user.err QT_OBEAPP: /lib/libc.so.6 [0xdf07824]

无需调用 qtobeapp_setStyleSheet(STYLE_SHEET_WITH_BLUE_SCREEN);,一切正常。

分段错误的原因可能是什么?

或

如何更改背景图片。

谢谢

【问题讨论】:

  • 我会为此使用信号和插槽,以便更改发生在 GUI 线程中。
  • 在主线程调用函数。
  • 您不需要全局静态 QApplication 指针。它已经以qApp 的形式提供给您。您可以使用QMetaObject::invokeMethod 从不同的线程调用QApplication::setStylesheet。

标签: c++ qt segmentation-fault


【解决方案1】:

谢谢大家,

我使用 QMetaObject::invokeMethod 来调用 QApplication::setStylesheet 并且成功了。

正是我通过 QMetaObject::invokeMethod 调用了 qtobeapp_setStyleSheet(int typeOfSheet)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2021-12-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2021-05-25
    相关资源
    最近更新 更多