PDA

View Full Version : Why my setter don't work?



gboelter
8th November 2009, 06:36
Hello World!

I'm really sorry guys, I know that this is not really a Qt relatedt question, but I don't know any better forum where are so many C++ specialist then here ...

I have a class, it's called 'Barverkauf' and until today everything is doing well.

barverkauf.h


class Barverkauf : public QDialog, public Ui::UiBarverkauf
{
Q_OBJECT

MainWindow *myMainWindow;

public:
Barverkauf( MainWindow*, QString currentUser );

// some other stuff follows ...


barverkauf.cpp



#include "barverkauf.h"

Barverkauf::Barverkauf ( MainWindow *pMainWindow, QString currentUser )
: myMainWindow( pMainWindow )
{
frameHome = new QFrame( myMainWindow->centralwidget );
setupUi( frameHome );
frameHome->show();

// some other stuff follows ...
}



ui_barverkauf.h



class Ui_UiBarverkauf
{
public:
// some other stuff ...
QTableWidget *tableWidgetBarverkauf;
// some other stuff ...


May be the code is not pefect - I'm still a beginner - but I can add, edit and delete lines to/from the tableWidgetBarverkauf very well..



But then I've tried to implement a barcode-reader, and now I have a problem:

I've created:

readbarcode.h


class ReadBarcode : public QDialog, private Ui::UiReadBarcode
{
Q_OBJECT

public:
//ReadBarcode( QWidget *parent = 0 );
ReadBarcode();
// some other stuff ...
private:
Barverkauf *barverkauf;


and

readbarcode.cpp


ReadBarcode::ReadBarcode()
{
setupUi( this );
//some other stuff ...

barverkauf = new Barverkauf;
}



Here i've tried to call the setter in barverkauf.cpp ...



void ReadBarcode::addArtikel()
// some other stuff ...
barverkauf->addPos( id, artikel, menge, einzel );
// some other stuff ...


Then I've added a second constructor to class Barverkauf like this:

barverkauf.h


class Barverkauf : public QDialog, public Ui::UiBarverkauf
{
Q_OBJECT

MainWindow *myMainWindow;

public:
Barverkauf( MainWindow*, QString currentUser );
Barverkauf();


barverkauf.cpp



#include "barverkauf.h"

Barverkauf::Barverkauf ( MainWindow *pMainWindow, QString currentUser )
: myMainWindow( pMainWindow )
{
frameHome = new QFrame( myMainWindow->centralwidget );
setupUi( frameHome );
frameHome->show();

// some other stuff follows ...
}

Barverkauf::Barverkauf()
{
}


and a setter like this:



void Barverkauf::addPos( QString id, QString artikel, QString menge, QString einzel )
{
qDebug() << id;
qDebug() << artikel;
qDebug() << menge;
qDebug() << einzel;

int row = tableWidgetBarverkauf->rowCount();

qDebug() << "row" << row;

// Eine neue Reihe hinzufuegen ...
tableWidgetBarverkauf->insertRow( row );
tableWidgetBarverkauf->setItem( row, 0, new QTableWidgetItem() );
tableWidgetBarverkauf->setItem( row, 1, new QTableWidgetItem() );
// some other code ...


Looks like the code is working now, but the lines are added to the second tableWidgetBarverkauf I have created in my copy-constructor.

In other words, it's working, but I can't see it on the screen!

Sorry, but I've never worked with a second constructor before ...

ChrisW67
8th November 2009, 08:06
The three lines of code in your original constructor set up the Qt Designer UI and show it. When you create another instance of the Barverkauf class with the bare constructor none of this UI setup is done and this is probably why you are not seeing anything.

I'm no C++ guru, and I'm not entirely sure what you are expecting to see/have happen.

gboelter
8th November 2009, 08:16
The three lines of code in your original constructor set up the Qt Designer UI and show it. When you create another instance of the Barverkauf class with the bare constructor none of this UI setup is done and this is probably why you are not seeing anything.

Thanks for the reply.

This was my idea too, but without these lines my setter can't see the tableWidget.

I'm sure it's easy to fix, but I don't have an idea how ...

ChrisW67
8th November 2009, 08:29
At line 6 in readbarcode.cpp you create a new, independent instance of the Barverkauf class using the constructor Barverkauf::Barverkauf(), which does not set up the UI in that instance as far as I can tell. ReadBarcode uses this private instance of the Barverkauf.

Are you trying to get two separate Barverkauf windows or have the ReadBarcode class access an existing one?

gboelter
8th November 2009, 08:41
Are you trying to get two separate Barverkauf windows or have the ReadBarcode class access an existing one?

No, there is only one window 'Barverkauf'. From there I would like to open a seperate snall gui - class ReadBarcode() - for my scan results.

After each scan, if the scan was successfull, the data should be added to the tableWidget inside the window 'Barverkauf".

ChrisW67
8th November 2009, 10:48
Your ReadBarcode class should be given some sort of access to the model that your table view is coming from. You could create your ReadBarcode objects with a pointer Barverkauf object or directly to the TableWidget/model you wish to update:

class ReadBarcode : public QDialog, private Ui::UiReadBarcode
{
Q_OBJECT

public:
//ReadBarcode( QWidget *parent = 0 );
ReadBarcode(Barverkauf *b);
// some other stuff ...
private:
Barverkauf *barverkauf;
}



ReadBarcode::ReadBarcode(Barverkauf *b)
{
setupUi( this );
//some other stuff ...

barverkauf = b;
}
and leave the rest of ReadBarcode unchanged.

You could consider separating the model underlying the QTableWidget (making it a QTableView) and share that model between the two classes.

gboelter
8th November 2009, 17:07
Chris,

what else can I say then THANK YOU, THANK YOU, THANK YOU ....

I was fighting with that for almost 2 days and I don't like to know how many days more without your help.

Thanks again.

Guenther
Davao City, Philippines, Planet Earth, 29.0 °C

ChrisW67
8th November 2009, 21:27
You're welcome,

Chris
Brisbane, Australia, Planet Earth, 25°C... beautiful one day, perfect the next ;)