【问题标题】:Why am I not getting a display in my Qt Widget?为什么我的 Qt Widget 没有显示?
【发布时间】:2023-12-20 23:53:01
【问题描述】:

当我运行它时,它会立即完成并且不显示任何内容。我找不到任何错误,#qt 上也没有人可以。我有其他应用程序运行良好,所以我不确定。它与 createForm 调用有关,如果我在构造函数中省略该调用,我确实会显示默认的 QWidget。

captchit.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2011-02-26T20:58:23
    #
    #-------------------------------------------------

QT += 核心 gui 网络

目标 = 验证码 模板 = 应用程序

SOURCES += main.cpp\ 小部件.cpp

标题 += 小部件.h

ma​​in.cpp

    #include "QtGui/QApplication"
    #include "widget.h"

int main(int argc, char *argv[]) { QApplication a(argc, argv); 小部件 mainWidget; mainWidget.show();

return a.exec();

}

widget.h

    #ifndef WIDGET_H
    #define WIDGET_H

#include <QWidget>

类 QPixmap; QLabel 类; QLineEdit 类; 类 QPushButton;

类小部件:公共 QWidget { Q_OBJECT

公开: 小部件(QWidget *parent = 0);

私人插槽: 无效 on_refreshButton_pressed(); 无效 on_submitButton_pressed(); 无效 on_closeButton_pressed();

私人: QPixmap 验证码图像; QLabel *imageLabel; QLabel *statusLabel; QLineEdit *captchaLineEdit; QPushButton *提交按钮; QPushButton *刷新按钮; QPushButton *closeButton;

void createForm();
void createActions();
void getCaptcha();
void submitCaptcha();

};

endif // WIDGET_H

widget.cpp

    #include "widget.h"
    #include "QtNetwork/QtNetwork"
    #include "QtGui"

void Widget::on_refreshButton_pressed() { ; }

void Widget::on_submitButton_pressed() { ; }

void Widget::on_closeButton_pressed() { ; }

// 创建 UI 组件 无效小部件::createForm() { // 创建主布局 QVBoxLayout *mainLayout = new QVBoxLayout(this);

// // 将验证码像素图设置为 imageLabel 用于显示 // imageLabel->setPixmap(captchaImage);

// Create Buttons
QVBoxLayout *buttonLayout = new QVBoxLayout();
submitButton->setText("Submit");
refreshButton->setText("Refresh");
closeButton->setText("Close");
buttonLayout->addWidget(submitButton);
buttonLayout->addWidget(refreshButton);
buttonLayout->addWidget(closeButton);

// Complete Layouts
// lineEdit & submitStatus
QVBoxLayout *lineEditLayout = new QVBoxLayout();
lineEditLayout->addStretch();
lineEditLayout->addWidget(statusLabel);
lineEditLayout->addWidget(captchaLineEdit);
lineEditLayout->addStretch();

// Create Bottom Layout
QHBoxLayout *bottomLayout = new QHBoxLayout();
bottomLayout->addLayout(lineEditLayout);
bottomLayout->addLayout(buttonLayout);

// Add to mainLayout

// mainLayout->addWidget(imageLabel); mainLayout->addLayout(bottomLayout); 设置布局(主布局); }

// 绑定槽和信号 无效小部件::createActions() { ; }

void Widget::getCaptcha() { ; }

void Widget::submitCaptcha() { ; }

Widget::Widget(QWidget *parent) : QWidget(父) { 创建表单(); // createActions(); }

【问题讨论】:

  • 这不是你的问题,但是为什么你有一堆带分号的空语句?

标签: c++ qt qt4


【解决方案1】:

在使用私有类成员之前,您可能需要在构造函数中初始化它们。

Widget::Widget(QWidget* parent) :
    QWidget(parent)
{
    captchaImage = new QPixmap;
    imageLabel = new QLabel(this);
    statusLabel = new QLabel(this);
    captchaLineEdit = new QLineEdit(this);
    submitButton = new QPushButton(this);
    refreshButton = new QPushButton(this);
    closeButton = new QPushButton(this);

    createForm();
//    createActions();
}

还请注意,QPixmap 不是从 QObject 派生的,因此您必须手动将其删除。最好从类中删除 QPixmap *captchaImage 成员并在代码中使用临时 QPixmap 对象。

【讨论】:

  • 是的,早一点意识到这一点,忘记来更新了。 Drr,如此明显的错误。
【解决方案2】:

糟糕,这是因为我忘记初始化所有组件,herp derp。

【讨论】:

    最近更新 更多