Qt Designer 4.6 中使用自定义小部件时出现分段错误

发布于 2024-08-17 21:16:11 字数 4169 浏览 7 评论 0原文

在 Qt Designer 4.6 中使用新的 Qt Widget 时,我遇到了分段错误。尝试预览新小部件时会出现问题。

使用 gdb 时,我发现问题出在 qdesigner_internal::WidgetFactory::applyStyleToTopLevel: 中,

Program received signal SIGSEGV, Segmentation fault.
qdesigner_internal::WidgetFactory::applyStyleToTopLevel (style=0x0, widget=0x1829df0) at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp:777
777 /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp: No such file or directory.
 in /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp
(gdb) bt
#0  qdesigner_internal::WidgetFactory::applyStyleToTopLevel (style=0x0, widget=0x1829df0) at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp:777
#1  0x00007ffff7475bed in qdesigner_internal::QDesignerFormBuilder::createPreview (fw=, styleName=..., appStyleSheet=..., deviceProfile=, scriptErrors=
    0x7fffffffbee0, errorMessage=0x7fffffffc3f0) at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/qdesigner_formbuilder.cpp:404
#2  0x00007ffff7476773 in qdesigner_internal::QDesignerFormBuilder::createPreview (fw=0x0, styleName=..., appStyleSheet=..., deviceProfile=..., errorMessage=0x0)
    at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/qdesigner_formbuilder.cpp:439
#3  0x00007ffff7532b27 in qdesigner_internal::PreviewManager::createPreview (this=0x837f20, fw=0x1879200, pc=..., deviceProfileIndex=-1, errorMessage=0x7fffffffc3f0, initialZoom=-1)
    at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/previewmanager.cpp:686
#4  0x00007ffff75343cf in qdesigner_internal::PreviewManager::showPreview (this=0x837f20, fw=0x1879200, pc=..., deviceProfileIndex=-1, errorMessage=0x7fffffffc3f0)
    at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/previewmanager.cpp:760
#5  0x00007ffff753472f in qdesigner_internal::PreviewManager::showPreview (this=0x837f20, fw=0x1879200, style=..., deviceProfileIndex=-1, errorMessage=0x7fffffffc3f0)
    at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/previewmanager.cpp:659

因为那里传递了一个空指针:


void WidgetFactory::applyStyleToTopLevel(QStyle *style, QWidget *widget)
{
    const QPalette standardPalette = style->standardPalette();
    if (widget->style() == style && widget->palette() == standardPalette)
        return;
//....
}

我是 Qt 的新手,这是我的第一个自定义小部件。有人有解决这个问题的线索吗?

这是我的小部件代码


MBICInput::MBICInput(QWidget *parent) : QStackedWidget(parent){
  displayPage = new QWidget();
  displayPage->setObjectName(QString::fromUtf8("displayPage"));

  inputLB = new QLabel(displayPage);
  inputLB->setObjectName(QString::fromUtf8("inputLabel"));
  inputLB->setCursor(QCursor(Qt::PointingHandCursor));

  addWidget(displayPage);

  EditPage = new QWidget();
  EditPage->setProperty("EditInputLine", QVariant(true));
  EditPage->setObjectName(QString::fromUtf8("EditPage"));

  inputInput = new QLineEdit(EditPage);
  inputInput->setGeometry(QRect(5, 10, 231, 25));
  inputInput->setObjectName(QString::fromUtf8("input"));

  addWidget(EditPage);

  _animation = new QString("");
  _message   = new QString("Message");
  _validator = new QRegExpValidator(QRegExp("[a-zA-Z]+"), this);

}

MBICInput::~MBICInput() {
}

QValidator::State MBICInput::validate(QString &text, int &pos) const{
    return _validator->validate(text, pos);
}

void MBICInput::paintEvent(QPaintEvent *) {
  QPainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing);
}


QSize MBICInput::minimumSizeHint() const{
   return QSize(200, 40);
}

QSize MBICInput::sizeHint() const{
   return QSize(200, 40);
}

void MBICInput::setAnimation(const QString &animation){
  *_animation = animation;
  update();
}

QString MBICInput::animation() const{
   return *_animation;
}

void MBICInput::setMessage(const QString &message){
  *_message = message;
  update();
}

QString MBICInput::message() const{
   return *_message;
}

void MBICInput::mousePressEvent(QMouseEvent *event){
  if(currentIndex()==0){
    setCurrentIndex(1);
  }else{
    setCurrentIndex(0);
  }
   update();
}

I had a segmentation fault when using my new Qt Widget with Qt Designer 4.6. The problem arise when trying to preview the new widget.

when using gdb I found that the problem is in qdesigner_internal::WidgetFactory::applyStyleToTopLevel:


Program received signal SIGSEGV, Segmentation fault.
qdesigner_internal::WidgetFactory::applyStyleToTopLevel (style=0x0, widget=0x1829df0) at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp:777
777 /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp: No such file or directory.
 in /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp
(gdb) bt
#0  qdesigner_internal::WidgetFactory::applyStyleToTopLevel (style=0x0, widget=0x1829df0) at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp:777
#1  0x00007ffff7475bed in qdesigner_internal::QDesignerFormBuilder::createPreview (fw=, styleName=..., appStyleSheet=..., deviceProfile=, scriptErrors=
    0x7fffffffbee0, errorMessage=0x7fffffffc3f0) at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/qdesigner_formbuilder.cpp:404
#2  0x00007ffff7476773 in qdesigner_internal::QDesignerFormBuilder::createPreview (fw=0x0, styleName=..., appStyleSheet=..., deviceProfile=..., errorMessage=0x0)
    at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/qdesigner_formbuilder.cpp:439
#3  0x00007ffff7532b27 in qdesigner_internal::PreviewManager::createPreview (this=0x837f20, fw=0x1879200, pc=..., deviceProfileIndex=-1, errorMessage=0x7fffffffc3f0, initialZoom=-1)
    at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/previewmanager.cpp:686
#4  0x00007ffff75343cf in qdesigner_internal::PreviewManager::showPreview (this=0x837f20, fw=0x1879200, pc=..., deviceProfileIndex=-1, errorMessage=0x7fffffffc3f0)
    at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/previewmanager.cpp:760
#5  0x00007ffff753472f in qdesigner_internal::PreviewManager::showPreview (this=0x837f20, fw=0x1879200, style=..., deviceProfileIndex=-1, errorMessage=0x7fffffffc3f0)
    at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/previewmanager.cpp:659

because a null pointer was passed there:


void WidgetFactory::applyStyleToTopLevel(QStyle *style, QWidget *widget)
{
    const QPalette standardPalette = style->standardPalette();
    if (widget->style() == style && widget->palette() == standardPalette)
        return;
//....
}

I'm new in Qt and this is my first custom widget. does anybody have a clue for solving this.

here is my widget code


MBICInput::MBICInput(QWidget *parent) : QStackedWidget(parent){
  displayPage = new QWidget();
  displayPage->setObjectName(QString::fromUtf8("displayPage"));

  inputLB = new QLabel(displayPage);
  inputLB->setObjectName(QString::fromUtf8("inputLabel"));
  inputLB->setCursor(QCursor(Qt::PointingHandCursor));

  addWidget(displayPage);

  EditPage = new QWidget();
  EditPage->setProperty("EditInputLine", QVariant(true));
  EditPage->setObjectName(QString::fromUtf8("EditPage"));

  inputInput = new QLineEdit(EditPage);
  inputInput->setGeometry(QRect(5, 10, 231, 25));
  inputInput->setObjectName(QString::fromUtf8("input"));

  addWidget(EditPage);

  _animation = new QString("");
  _message   = new QString("Message");
  _validator = new QRegExpValidator(QRegExp("[a-zA-Z]+"), this);

}

MBICInput::~MBICInput() {
}

QValidator::State MBICInput::validate(QString &text, int &pos) const{
    return _validator->validate(text, pos);
}

void MBICInput::paintEvent(QPaintEvent *) {
  QPainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing);
}


QSize MBICInput::minimumSizeHint() const{
   return QSize(200, 40);
}

QSize MBICInput::sizeHint() const{
   return QSize(200, 40);
}

void MBICInput::setAnimation(const QString &animation){
  *_animation = animation;
  update();
}

QString MBICInput::animation() const{
   return *_animation;
}

void MBICInput::setMessage(const QString &message){
  *_message = message;
  update();
}

QString MBICInput::message() const{
   return *_message;
}

void MBICInput::mousePressEvent(QMouseEvent *event){
  if(currentIndex()==0){
    setCurrentIndex(1);
  }else{
    setCurrentIndex(0);
  }
   update();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无妨# 2024-08-24 21:16:11

applyStyleToTopLevel 函数中,您使用了小部件指针,但没有验证它是否是有效的指针。因此,当使用 NULL 小部件指针执行 widget->style 时,它会崩溃。

In the applyStyleToTopLevel function, you are using the widget pointers without verifying it's a valid pointer. So when doing widget->style with a NULL widget pointer it will crash.

孤凫 2024-08-24 21:16:11

仅供参考,我刚刚在 Qt 版本 4.5.3-9 下编译了相同的代码,它在 4.5 设计器中运行得很好。可能是 Qt 4.6 的一个错误......

Just for information I just compiled the same code under the Qt version 4.5.3-9 and it worked just fine with the 4.5 designer. may be it's a Qt 4.6 bug...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文