[英]Why am I not getting a display in my Qt Widget?
When I run it it will just finish right away and not show anything. 当我运行它时,它将立即完成,并没有显示任何东西。 I can't find anything wrong w/it and no one on #qt could either. 我找不到任何错误,#qt也没有人。 I've got other apps working fine so I'm not sure. 我有其他应用程序正常工作,所以我不确定。 It's got something to do with the createForm call, if I omit that call in the constructor I do get a default QWidget displayed. 它与createForm调用有关,如果我在构造函数中省略了该调用,我会显示默认的QWidget。
captchit.pro 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 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 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 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(); }
You need to initialize your private class members, probably in your constructor, before you use them. 在使用它们之前,您可能需要在构造函数中初始化您的私有类成员。
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();
}
Also note that QPixmap does not derive from QObject, so you'll have to delete it manually. 另请注意,QPixmap不是从QObject派生的,因此您必须手动删除它。 It may be better to remove the QPixmap *captchaImage
member from your class and use temporary QPixmap objects in your code. 从类中删除QPixmap *captchaImage
成员并在代码中使用临时QPixmap对象可能更好。
哎呀,这是因为我忘了初始化我的所有组件,herp derp。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.