首页 文章

为什么我没有在Qt Widget中显示?

提问于
浏览
0

当我运行它时,它将立即完成,并没有显示任何东西 . 我找不到任何错误,#qt也没有人 . 我有其他应用程序正常工作,所以我不确定 . 它与createForm调用有关,如果我在构造函数中省略了该调用,我会显示默认的QWidget .

captchit.pro ``

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

 QT       += core gui network 

 TARGET = captchit
TEMPLATE = app 

 SOURCES += main.cpp\
        widget.cpp 

 HEADERS  += widget.h

main.cpp ``

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

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

 return a.exec();
 

 }

widget.h ``

#ifndef WIDGET_H
    #define WIDGET_H 

 #include <QWidget>
 

 class QPixmap;
class QLabel;
class QLineEdit;
class QPushButton; 

 class Widget : public QWidget
{
    Q_OBJECT 

 public:
    Widget(QWidget *parent = 0); 

 private slots:
    void on_refreshButton_pressed();
    void on_submitButton_pressed();
    void on_closeButton_pressed(); 

 private:
    QPixmap captchaImage;
    QLabel *imageLabel;
    QLabel *statusLabel;
    QLineEdit *captchaLineEdit;
    QPushButton *submitButton;
    QPushButton *refreshButton;
    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()
{
    ;
} 

 // Create UI Components
void Widget::createForm()
{
    // Create Main Layout
    QVBoxLayout *mainLayout = new QVBoxLayout(this); 

 //    // set captcha pixmap to imageLabel for displaying
//    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);
    setLayout(mainLayout);
} 

 // Bind Slots and Signals
void Widget::createActions()
{
    ;
} 

 void Widget::getCaptcha()
{
    ;
} 

 void Widget::submitCaptcha()
{
    ;
} 

 Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    createForm();
//    createActions();
}

2 回答

  • 2

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

    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对象可能更好 .

  • 0

    哎呀,这是因为我忘了初始化我的所有组件,herp derp .

相关问题