Results 1 to 5 of 5

Thread: Dynamic For generation Problems.

  1. #1
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Dynamic For generation Problems.

    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
    Qt Code:
    1. #ifndef FRM_MAINTENANCE_ALL_H
    2. #define FRM_MAINTENANCE_ALL_H
    3.  
    4. #include <QWidget>
    5. #include <QDialog>
    6. #include <QtGui>
    7. #include <QApplication>
    8. #include <QDebug>
    9. #include <QMessageBox>
    10. #include <QList>
    11.  
    12. class frm_maintenance_all : public QWidget
    13. {
    14. Q_OBJECT
    15. public:
    16. explicit frm_maintenance_all(QWidget *parent = 0);
    17.  
    18. signals:
    19.  
    20. public slots:
    21.  
    22. private slots:
    23. void frm_create(QString tablename);
    24. void btn_process();
    25.  
    26. private:
    27.  
    28.  
    29.  
    30. };
    31.  
    32. #endif // FRM_MAINTENANCE_ALL_H
    To copy to clipboard, switch view to plain text mode 

    .cpp file
    Qt Code:
    1. #include "frm_maintenance_all.h"
    2.  
    3. frm_maintenance_all::frm_maintenance_all(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. frm_create("test");
    7.  
    8. }
    9.  
    10. void frm_maintenance_all::frm_create(QString tablename)
    11. {
    12.  
    13. QVBoxLayout *myhoz = new QVBoxLayout;
    14. QStringList mylist;
    15. mylist<< "description" << "name" << "id";
    16.  
    17. int i;
    18. QLineEdit *myline;
    19. for(i=1;i<=mylist.length();i++)
    20. {
    21. myline = new QLineEdit;
    22. myline->setText("Text " + QString::number(i)); //just setting text for testing
    23. myhoz->addWidget(myline);
    24. }
    25.  
    26. QPushButton *btn_process_form = new QPushButton;
    27. myhoz->addWidget(btn_process_form);
    28.  
    29. setLayout(myhoz);
    30. 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
    31. connect(btn_process_form,SIGNAL(clicked()),this,SLOT(btn_process()));
    32.  
    33. setWindowTitle("Test");
    34. show();
    35.  
    36. }
    37.  
    38. void frm_maintenance_all::btn_process()
    39. {
    40. //This does not work (expected to show line 3 option).
    41. qDebug()<< myline->text();
    42.  
    43. //this function fails left uncomented here for reading
    44. QLineEdit* mydyline = parentWidget()->findChild<QLineEdit*>("myline");
    45. qDebug()<< mydyline->text();
    46. // this also fails
    47. QLineEdit* mydyline = parent()->findChild<QLineEdit*>("myline");
    48. qDebug()<< mydyline->text();
    49. // this also fails
    50. QLineEdit* mydyline = findChild<QLineEdit*>("myline");
    51. qDebug()<< mydyline->text();
    52. // this also fails
    53. QLineEdit* mydyline = this->findChild<QLineEdit*>("myline");
    54. qDebug()<< mydyline->text();
    55. //FAILS WITH THIS ERROR
    56. //The program has unexpectedly finished.
    57.  
    58. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Dynamic For generation Problems.

    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.

    Quote Originally Posted by ShapeShiftme View Post
    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.

  3. #3
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Dynamic For generation Problems.

    Quote Originally Posted by ChrisW67 View Post
    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


    Quote Originally Posted by ChrisW67 View Post
    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

    Quote Originally Posted by ChrisW67 View Post
    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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Dynamic For generation Problems.

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MainWindow: public QMainWindow {
    5. Q_OBJECT
    6. public:
    7. MainWindow(QWidget *p = 0): QMainWindow(p) {
    8. QWidget *central = new QWidget(this); // central is a local varaibale
    9. label1 = new QLabel(this); // label1 is a member variable, a pointer to a label
    10. label2 = new QLabel(this);
    11.  
    12. QVBoxLayout *layout = new QVBoxLayout(central);
    13. layout->addWidget(label1);
    14. layout->addWidget(label2);
    15.  
    16. central->setLayout(layout);
    17. setCentralWidget(central);
    18.  
    19. doStuff(); // set starting labels
    20.  
    21. connect(&timer, SIGNAL(timeout()), SLOT(doStuff()));
    22. timer.start(1000); // actual object, use dot
    23. }
    24.  
    25. public slots:
    26. void doStuff() {
    27. static int count = 0; // a local variable
    28. label1->setText( QString::number(count) ); // pointer to object, use ->
    29. label2->setText( QString::number(count * count) );
    30. ++count;
    31. }
    32.  
    33. private:
    34. // member variables
    35. QLabel *label1; // pointer to a QLabel instance
    36. QLabel *label2;
    37. QTimer timer; // actual instance of a QTimer
    38. };
    39.  
    40. int main(int argc, char *argv[])
    41. {
    42. QApplication app(argc, argv);
    43.  
    44. MainWindow m;
    45. m.show();
    46. return app.exec();
    47. }
    48. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Dynamic For generation Problems.

    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.

    Qt Code:
    1. int i;
    2. for(i=1;i<=5;i++)
    3. {
    4. QLineEdit *myline;
    5. myline = new QLineEdit;
    6. myline->setText("Text " + QString::number(i));
    7. myhoz->addWidget(myline);
    8. }
    To copy to clipboard, switch view to plain text mode 

    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

Similar Threads

  1. Replies: 0
    Last Post: 29th March 2012, 19:56
  2. Replies: 3
    Last Post: 31st July 2011, 00:30
  3. Problems about Dynamic Language Switching
    By to_guliang in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2010, 00:17
  4. SVG Generation Problem
    By Jime22 in forum Qt Programming
    Replies: 0
    Last Post: 30th April 2008, 22:49
  5. Random no. generation using QT3??
    By darpan in forum General Programming
    Replies: 1
    Last Post: 8th August 2006, 12:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.