PDA

View Full Version : What am i doing wrong?



ayanda83
2nd January 2013, 08:31
The code below is suppose to display a label on a window but it doesn't, instead it displays a blank window. what am I doing wrong. Please help.

#ifndef PAYCLASS_H
#define PAYCLASS_H
#include <QMainWindow>
#include <QObject>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QTextEdit>
#include <QDate>
#include <QCalendarWidget>
#include <QTimeEdit>
#include <QHBoxLayout>

class PayClass : public QMainWindow
{
Q_OBJECT
public:
explicit PayClass();
double calculateHrs(int numOfHrs);
double calculatePay();

signals:

public slots:
private:
QPushButton* Calculate;
QGridLayout* Layout;
QHBoxLayout* HLayout;
QLabel* Label1, *Label2, *Label3, *Label4, *Mon, *Tue, *Wed, *Thur, *Fri, *Sat;
QTextEdit* txtEdit1, *txtEdit2;
QDate *dateOnDuty;
QCalendarWidget* Calender;
QTimeEdit* Time1, *StartOfLunch, *endOfLunch, *Time3;


};

#endif // PAYCLASS_H

#include <QtGui/QApplication>
#include "payclass.h"

int main(int argc, char* argv[])
{
QApplication App(argc, argv);
PayClass myApp;

myApp.show();

system("PAUSE");

return 0;
}

#include "payclass.h"

PayClass::PayClass()
{
setWindowTitle("My Hours");

Label1 = new QLabel("Clocking In Time");
Label2 = new QLabel("Start Of Lunch");
Label3 = new QLabel("End Of Lunch");
Label4 = new QLabel("Clocking Out Time");

Calculate = new QPushButton("Calculate");



HLayout = new QHBoxLayout();
Layout = new QGridLayout();

Layout->addWidget(Label1, 0, 0);

setLayout(Layout);


}

Lesiok
2nd January 2013, 09:00
Instead of

system("PAUSE");
return 0;
use

return App.exec();
You need a working event loop.

ayanda83
2nd January 2013, 09:09
I've done that and it still doesn't work.

Santosh Reddy
2nd January 2013, 09:58
PayClass is derivered from QMainWindow. QMainWindow has a special Layout. So Replace this code

PayClass::PayClass()
{
...
setLayout(Layout); // <<<<<<<<<<<<<<<<<<<<<< Remove
}
with

PayClass::PayClass()
{
...
// <<<<<<<<<<<<<<<<<<<<<< Add following
QWidget * widget = new QWidget(this);
widget->setLayout(Layout);
setCentralWidget(widget);
}