PDA

View Full Version : Dynamic For generation Problems.



ShapeShiftme
19th May 2012, 08:34
Good day all.
This post is an addon to a previous one where i had trouble creating the layout but not that is sorted. Here is my problem

I have a list of 3 items that i would like edited "They happen to corrispond to database field in mysql"
The amount of items can change so therefore i dont want to create the form statically then i would have to create 35 forms in stead of just 1 and pass the list to it.

So this is what i have got to create the form "Im not using a ui as it must be dynamic"

.h file

#ifndef FRM_MAINTENANCE_ALL_H
#define FRM_MAINTENANCE_ALL_H

#include <QWidget>
#include <QDialog>
#include <QtGui>
#include <QApplication>
#include <QDebug>
#include <QMessageBox>
#include <QList>

class frm_maintenance_all : public QWidget
{
Q_OBJECT
public:
explicit frm_maintenance_all(QWidget *parent = 0);

signals:

public slots:

private slots:
void frm_create(QString tablename);
void btn_process();

private:



};

#endif // FRM_MAINTENANCE_ALL_H

.cpp file


#include "frm_maintenance_all.h"

frm_maintenance_all::frm_maintenance_all(QWidget *parent) :
QWidget(parent)
{
frm_create("test");

}

void frm_maintenance_all::frm_create(QString tablename)
{

QVBoxLayout *myhoz = new QVBoxLayout;
QStringList mylist;
mylist<< "description" << "name" << "id";

int i;
QLineEdit *myline;
for(i=1;i<=mylist.length();i++)
{
myline = new QLineEdit;
myline->setText("Text " + QString::number(i)); //just setting text for testing
myhoz->addWidget(myline);
}

QPushButton *btn_process_form = new QPushButton;
myhoz->addWidget(btn_process_form);

setLayout(myhoz);
btn_process_form->setText(myline->text()); //it set the text to the last loop as expected but would like to be able to choose which box i want
connect(btn_process_form,SIGNAL(clicked()),this,SL OT(btn_process()));

setWindowTitle("Test");
show();

}

void frm_maintenance_all::btn_process()
{
//This does not work (expected to show line 3 option).
qDebug()<< myline->text();

//this function fails left uncomented here for reading
QLineEdit* mydyline = parentWidget()->findChild<QLineEdit*>("myline");
qDebug()<< mydyline->text();
// this also fails
QLineEdit* mydyline = parent()->findChild<QLineEdit*>("myline");
qDebug()<< mydyline->text();
// this also fails
QLineEdit* mydyline = findChild<QLineEdit*>("myline");
qDebug()<< mydyline->text();
// this also fails
QLineEdit* mydyline = this->findChild<QLineEdit*>("myline");
qDebug()<< mydyline->text();
//FAILS WITH THIS ERROR
//The program has unexpectedly finished.

}


So there are 2 things i would like to please know :

1) How can i create a unique id or something for the LineEdit so i can reference it
eg so i could set the push button to line 2's text instead of the last one.

2) How do i then access the values From sub Functions

Help with this regard Will be awsome

Thanks very much

ChrisW67
19th May 2012, 09:17
Your code as presented will not compile. "myline" at cpp line 41 is not declared anywhere. Given that you seem to have had it compile you must have a "myline" declared as a member variable that you did not show us. This variable is in no way related to the "myline" variable declared at line 18 and used in the function frm_create(). Your compiler is probably warning you that the "myline" in frm_create() masks an earlier definition.

Line 41 crashes because myline is null or invalid.
Line 44 can crash because parentWidget() is null. If it doesn't crash for that reason then it will return null because you have not called setObjectName() on any of your widgets, so there is no widget named "myline". This will make line 45 crash.
Ditto line 47, 50, and 53.



1) How can i create a unique id or something for the LineEdit so i can reference it
eg so i could set the push button to line 2's text instead of the last one.

Keep a pointer to the widget as a member variable and access the widget through its pointer.


2) How do i then access the values From sub Functions

You access member and local variables by name.
If the variable is an object (or reference to one) you can access its member functions using object.function() notation.
If the variable is a pointer to an object then you can access its member functions using object->function() notation.
You can only access variables that are in scope.

All of this is basic C++ and nothing particularly to do with Qt.

ShapeShiftme
19th May 2012, 09:40
Line 41 crashes because myline is null or invalid.
Line 44 can crash because parentWidget() is null. If it doesn't crash for that reason then it will return null because you have not called setObjectName() on any of your widgets, so there is no widget named "myline". This will make line 45 crash.
Ditto line 47, 50, and 53.

I understand why they crashed this was more of an eample of what im trying to do do and was hoping for the correct syntax to use




Keep a pointer to the widget as a member variable and access the widget through its pointer.


Could you please give me a small example for this



If the variable is a pointer to an object then you can access its member functions using object->function() notation.


Perhaps an example for this too please.

Regards

ChrisW67
19th May 2012, 09:58
#include <QtGui>
#include <QDebug>

class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p) {
QWidget *central = new QWidget(this); // central is a local varaibale
label1 = new QLabel(this); // label1 is a member variable, a pointer to a label
label2 = new QLabel(this);

QVBoxLayout *layout = new QVBoxLayout(central);
layout->addWidget(label1);
layout->addWidget(label2);

central->setLayout(layout);
setCentralWidget(central);

doStuff(); // set starting labels

connect(&timer, SIGNAL(timeout()), SLOT(doStuff()));
timer.start(1000); // actual object, use dot
}

public slots:
void doStuff() {
static int count = 0; // a local variable
label1->setText( QString::number(count) ); // pointer to object, use ->
label2->setText( QString::number(count * count) );
++count;
}

private:
// member variables
QLabel *label1; // pointer to a QLabel instance
QLabel *label2;
QTimer timer; // actual instance of a QTimer
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"

ShapeShiftme
21st May 2012, 12:53
That for the code. but all that i can do.

What i need to do is create the form dynamically but from your code you know how many input boxes are created.

In theory im might have 30 to create so i would like to loop them in creating and that i dont know how to reference them in this case.

for example i want to create 5 lineedits.



int i;
for(i=1;i<=5;i++)
{
QLineEdit *myline;
myline = new QLineEdit;
myline->setText("Text " + QString::number(i));
myhoz->addWidget(myline);
}



now i KNOW this WONT work but this is what i want to do but i would not know how to do it.

Then what i would like to do is be able to reference the objects from another function.

Am i making myself clear at all
regards