Results 1 to 6 of 6

Thread: How to create an Insert/Submit button for a form.

  1. #1
    Join Date
    Jul 2006
    Location
    Atlanta, GA
    Posts
    86
    Thanks
    26
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Red face How to create an Insert/Submit button for a form.

    Hello Everyone... Long time reader, first time poster. I must say, I have learned so much from this forum, and I thank you all. I have a form that I will be entering info about the pieces of music I write, and they will be saved into a MySQL database on another machine. I can connect to the database just fine. I am trying to create a button on the form that will insert the info on the form into the database:

    model->insertRecord(-1, rec);

    Would I have to create a seperate header file, or could I create this in the main.cpp? I tried adding a Submit Button like these two ways, but both did not work:

    QPushButton *submitButton = new QPushButton("Submit");
    QObject::connect(submitButton, SIGNAL(clicked()), &app, SLOT(model->insertRecord(-1, rec)));
    or
    QPushButton *submitButton = new QPushButton("Submit");
    QObject::connect(submitButton, SIGNAL(clicked()), &app, SLOT(exec(model->insertRecord(-1, rec))));

    If I put that directly into the code like this:
    QSqlTableModel *model = new QSqlTableModel;
    model->setTable("main");
    QSqlRecord rec;
    rec.append(QSqlField("piece_name", QVariant::String));
    rec.append(QSqlField("status", QVariant::String));
    rec.setValue("piece_name", pieceNameLineEdit->text());
    rec.setValue("status", statusComboBox->currentText());
    model->insertRecord(-1, rec);
    it will create a new record with nothing in the piece_name field, and "In Progress" in the status field.

    Any advice would be very much appreciated.

    Brian
    QT 4 on Windows XP
    fnmblot
    --------------------------------------
    Gee Ricky, I'm sorry your mom blew up.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create an Insert/Submit button for a form.

    You need a custom slot for this. To make one, you need a class.

    http://doc.trolltech.com/4.1/tutorial-t4.html

  3. The following user says thank you to jacek for this useful post:

    fnmblot (3rd August 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create an Insert/Submit button for a form.

    Read the section in the Qt docs on 'signals and slots' very carefully. This:

    Qt Code:
    1. QObject::connect(submitButton, SIGNAL(clicked()), &app, SLOT(model->insertRecord(-1, rec)));
    To copy to clipboard, switch view to plain text mode 
    is not valid. You cannot pass parameters to slots in a connect statement. Also, the QApplication object has no slot called "model->insertRecord()" (which would be impossible anyway). It should be

    Qt Code:
    1. QObject::connect(submitButton, SIGNAL(clicked()), &myWidget, SLOT(insertRecordSlot()));
    To copy to clipboard, switch view to plain text mode 

    Where myWidget is an instance of your GUI form.
    Since the clicked() signal does not have any parameters, the slot you connect it to cannot have any parameters either.

    Then in your insertRecordSlot() implementation, you can do this:

    Qt Code:
    1. void MyWidget::insertRecordSlot()
    2. {
    3. ...
    4. model->insertRecord(-1, rec));
    5. }
    To copy to clipboard, switch view to plain text mode 

    In answer to your question of whether you will need to create a separate header file or not, that is just a question of good modular design, i.e. it is your decision. You will have to declare your MyWidget class in some header file, so that moc can process your slots declaration. Whether that is in main.h or MyClass.h or whatever.h is up to you...

    Also, it's usually cleaner to put your internal GUI connection code in the constructor of your GUI class and not outside of it in main (in most cases).
    Save yourself some pain. Learn C++ before learning Qt.

  5. The following user says thank you to Chicken Blood Machine for this useful post:

    fnmblot (3rd August 2006)

  6. #4
    Join Date
    Jul 2006
    Location
    Atlanta, GA
    Posts
    86
    Thanks
    26
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create an Insert/Submit button for a form.

    ok, so I did as suggested... read the tutorial. I created a class, and it compiles without errors. But when I click on the submit button, nothing happens. This is what I wrote:

    pieceInsert.h
    Qt Code:
    1. #ifndef PIECEINSERT_H
    2. #define PIECEINSERT_H
    3.  
    4. #include <QDialog>
    5.  
    6. class QLabel;
    7. class QLineEdit;
    8. class QComboBox;
    9.  
    10. class Dialog : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. Dialog(QWidget *parent = 0);
    16.  
    17. private slots:
    18. void insertRecordSlot();
    19.  
    20. private:
    21. QLabel *itemLabel;
    22. QLabel *pieceNameLabel;
    23. QLineEdit *pieceNameLineEdit;
    24. QLabel *statusLabel;
    25. QComboBox *statusComboBox;
    26. QPushButton *closeButton;
    27. QPushButton *submitButton;
    28. };
    29.  
    30. #endif
    To copy to clipboard, switch view to plain text mode 
    pieceInsert.cpp
    Qt Code:
    1. #include <QApplication>
    2. ...
    3. #include "pieceInsert.h"
    4.  
    5. Dialog::Dialog(QWidget *parent)
    6. : QDialog(parent)
    7. {
    8. // Window Title
    9. QWidget *window = new QWidget;
    10. window->setWindowTitle("NNBS Piece Entry");
    11. window->setFixedSize(450, 270);
    12.  
    13. // Piece Entry Section
    14. pieceNameLabel = new QLabel();
    15. pieceNameLabel->setText("Piece Name");
    16. pieceNameLineEdit = new QLineEdit();
    17. statusLabel = new QLabel();
    18. statusLabel->setText("Status");
    19. statusComboBox = new QComboBox();
    20. statusComboBox->addItem("In Progress");
    21. statusComboBox->addItem("Dormant");
    22. statusComboBox->addItem("Complete");
    23.  
    24. QHBoxLayout *h2BoxLayout = new QHBoxLayout;
    25. h2BoxLayout->addWidget(pieceNameLabel);
    26. h2BoxLayout->addWidget(pieceNameLineEdit);
    27. h2BoxLayout->addWidget(statusLabel);
    28. h2BoxLayout->addWidget(statusComboBox);
    29.  
    30. // **** Start Create Buttons ****//
    31. // Close Button Layout
    32. closeButton = new QPushButton("Close");
    33. QObject::connect(closeButton, SIGNAL(clicked()), qApp, SLOT(quit()));
    34. // Submit Button
    35. submitButton = new QPushButton("Submit");
    36. QObject::connect(submitButton, SIGNAL(clicked()), qApp, SLOT(insertRecordSlot()));
    37. // Create Buttons Layout
    38. QHBoxLayout *hBoxLayout = new QHBoxLayout;
    39. QSpacerItem *spacerItem = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    40. hBoxLayout->addItem(spacerItem);
    41. hBoxLayout->addWidget(submitButton);
    42. hBoxLayout->addWidget(closeButton);
    43. // **** End Buttons **** //
    44.  
    45. // Entire Layout
    46. QVBoxLayout *vBoxLayout = new QVBoxLayout;
    47. vBoxLayout->addLayout(h2BoxLayout);
    48. vBoxLayout->addLayout(hBoxLayout);
    49. window->setLayout(vBoxLayout);
    50. window->show();
    51. }
    52. void Dialog::insertRecordSlot()
    53. {
    54. // SQL Insert Record
    55. rec.append(QSqlField("piece_name", QVariant::String));
    56. rec.append(QSqlField("status", QVariant::String));
    57. rec.setValue("piece_name", pieceNameLineEdit->text());
    58. rec.setValue("status", statusComboBox->currentText());
    59. model = new QSqlTableModel;
    60. model->setTable("main");
    61. model->insertRecord(-1, rec);
    62. }
    To copy to clipboard, switch view to plain text mode 
    Any suggestions?
    fnmblot
    --------------------------------------
    Gee Ricky, I'm sorry your mom blew up.

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create an Insert/Submit button for a form.

    Quote Originally Posted by fnmblot
    But when I click on the submit button, nothing happens.
    You have connected clicked() signal to QApplication instance (qApp) instead of Dialog.

    It should be:
    Qt Code:
    1. connect(closeButton, SIGNAL(clicked()), this, SLOT(quit()));
    2. ...
    3. connect(submitButton, SIGNAL(clicked()), this, SLOT(insertRecordSlot()));
    To copy to clipboard, switch view to plain text mode 

    BTW. Qt should warn you that you try to connect to slot that doesn't exist. If you can't see the message on the console, you will have to compile your application in debug mode. To do this add CONFIG += debug to your .pro file. On windows your application should be compiled in both debug and release modes by default, but you need CONFIG += console, to actually see the console.

  8. The following user says thank you to jacek for this useful post:

    fnmblot (4th August 2006)

  9. #6
    Join Date
    Jul 2006
    Location
    Atlanta, GA
    Posts
    86
    Thanks
    26
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create an Insert/Submit button for a form.

    I never knew about the CONFIG += debug or console tricks.

    Thanks!

    Brian
    fnmblot
    --------------------------------------
    Gee Ricky, I'm sorry your mom blew up.

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.