Results 1 to 7 of 7

Thread: How to display my application gui

  1. #1
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How to display my application gui

    I have made an application and it has a Qtableview,four form fields of Qlinedit,Qgridlayout and five buttons.The application builds without any errors but exits when i try to run it with the windows error message : "smith.exe has encountered a problem and needs to close. We are sorry for the inconvenience.".

    Other examples i have run correctly.

    Here is the code.

    smith.h
    Qt Code:
    1. #ifndef SMITH_H
    2. #define SMITH_H
    3.  
    4. #include <QWidget>
    5.  
    6. QT_BEGIN_NAMESPACE
    7. class QLabel;
    8. class QLineEdit;
    9. class QTableView;
    10. QT_END_NAMESPACE
    11.  
    12. class smith : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. smith(QWidget *parent = 0);
    18.  
    19. private:
    20.  
    21. void setupModel();
    22.  
    23. QSqlTableModel *tableModel;
    24. QTableView *tableView;
    25. QLabel *fnLabel;
    26. QLabel *lnLabel;
    27. QLabel *countryLabel;
    28. QLabel *cityLabel;
    29. QLineEdit *fEdit;
    30. QLineEdit *lnEdit;
    31. QLineEdit *cEdit;
    32. QLineEdit *cityEdit;
    33. QPushButton *newButton;
    34. QPushButton *nextButton;
    35. QPushButton *previousButton;
    36. QPushButton *saveButton;
    37. QPushButton *deleteButton;
    38.  
    39. private slots:
    40. void on_deleteButton_clicked();
    41. void on_saveButton_clicked();
    42. void on_newButton_clicked();
    43. };
    44.  
    45. #endif // SMITH_H
    To copy to clipboard, switch view to plain text mode 

    smith.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include "smith.h"
    4.  
    5. smith::smith(QWidget *parent) :
    6. QWidget(parent)
    7. {
    8.  
    9. setupModel();
    10.  
    11. QLabel *fnLabel = new QLabel(tr("First Name:"));
    12. fEdit = new QLineEdit;
    13. fEdit->setReadOnly(true);
    14.  
    15. QLabel *lnLabel = new QLabel(tr("Last Name:"));
    16. lnEdit = new QLineEdit;
    17. lnEdit->setReadOnly(true);
    18.  
    19. QLabel *countryLabel = new QLabel(tr("Country:"));
    20. cEdit = new QLineEdit;
    21. cEdit->setReadOnly(true);
    22.  
    23. QLabel *cityLabel = new QLabel(tr("City:"));
    24. cityEdit = new QLineEdit;
    25. cityEdit->setReadOnly(true);
    26.  
    27. newButton = new QPushButton(tr("&New"));
    28. nextButton = new QPushButton(tr("&Next"));
    29. previousButton = new QPushButton(tr("&Previous"));
    30. saveButton = new QPushButton(tr("&Save"));
    31. deleteButton = new QPushButton(tr("&Delete"));
    32.  
    33. connect(newButton, SIGNAL(clicked()), this, SLOT(on_newButton_clicked()));
    34. connect(nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
    35. connect(previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
    36. connect(saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()));
    37. connect(deleteButton, SIGNAL(clicked()), this, SLOT(on_deleteButton_clicked()));
    38.  
    39. QGridLayout *mainLayout = new QGridLayout;
    40. mainLayout->addWidget(tableView);
    41. mainLayout->addWidget(fnLabel);
    42. mainLayout->addWidget(fEdit);
    43. mainLayout->addWidget(lnLabel);
    44. mainLayout->addWidget(lnEdit);
    45. mainLayout->addWidget(countryLabel);
    46. mainLayout->addWidget(cEdit);
    47. mainLayout->addWidget(cityLabel);
    48. mainLayout->addWidget(cityEdit);
    49. mainLayout->addWidget(newButton);
    50. mainLayout->addWidget(nextButton);
    51. mainLayout->addWidget(previousButton);
    52. mainLayout->addWidget(saveButton);
    53. mainLayout->addWidget(deleteButton);
    54.  
    55. setLayout(mainLayout);
    56. setWindowTitle(tr("Application Layout"));
    57.  
    58.  
    59. //Model setup
    60. tableModel = new QSqlTableModel(this);
    61. tableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
    62. tableModel->setTable("application");
    63. tableModel->select();
    64. tableModel->setHeaderData(0, Qt::Horizontal, tr("First Name"));
    65. tableModel->setHeaderData(1, Qt::Horizontal, tr("Last Name"));
    66. tableModel->setHeaderData(2, Qt::Horizontal, tr("Country"));
    67. tableModel->setHeaderData(3, Qt::Horizontal, tr("City"));
    68.  
    69. fnLabel->setBuddy(fEdit);
    70. lnLabel->setBuddy(lnEdit);
    71. countryLabel->setBuddy(cEdit);
    72. cityLabel->setBuddy(cityEdit);
    73.  
    74. //Mapper
    75. mapper = new QDataWidgetMapper(this);
    76. mapper->setModel(tableModel);
    77. mapper->addMapping(fEdit, 0);
    78. mapper->addMapping(lnEdit, 1);
    79. mapper->addMapping(cEdit, 2);
    80. mapper->addMapping(cityEdit, 3);
    81. mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    82. mapper->toFirst();
    83.  
    84.  
    85. //TableView
    86. tableView = new QTableView(this);
    87. tableView->setModel(tableModel);
    88. tableView->resizeColumnsToContents();
    89. tableView->setWindowTitle("Application Data");
    90. }
    91.  
    92. void smith::setupModel()
    93. {
    94. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    95. db.setDatabaseName("smith.DAT");
    96. if (!db.open()) {
    97. QMessageBox::critical(0, tr("Cannot open database"),
    98. tr("Unable to establish a database connection.\n"
    99. "This example needs SQLite support. Please read "
    100. "the Qt SQL driver documentation for information how "
    101. "to build it."), QMessageBox::Cancel);
    102. return;
    103. }
    104. }
    105. void smith::on_newButton_clicked()
    106. {
    107.  
    108. }
    109.  
    110.  
    111. void smith::on_saveButton_clicked()
    112. {
    113.  
    114. }
    115.  
    116. void smith::on_deleteButton_clicked()
    117. {
    118.  
    119. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "smith.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. smith smith;
    8. smith.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    How do i get the qtableview,the buttons and the forms to display correctly?.
    Last edited by thefatladysingsopera; 27th July 2011 at 16:35.

  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: How to display my application gui

    The crash is one of any number of things: missing DLLs and null pointer dereferencing are top of the list. Run the program in a debugger and look at the backtrace.

    You should put your table view into the layout.
    Your use of the grid layout is odd. Why the large column and row numbers?
    Lines 11, 15, 19, 23 of the cpp file declare locals that mask member variables. Not likely to be a problem as-is, but might be later of you access, for example, the fnLabel member variable and it is not initialised.

  3. #3
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to display my application gui

    Quote Originally Posted by ChrisW67 View Post
    You should put your table view into the layout.
    How can i do that?.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to display my application gui

    Check the order of the statements in your constructor. For instance, what does mapper in line 34 point to? And tableView in line 40.

  5. #5
    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: How to display my application gui

    Bringing a Private Message back to the thread. Please do not ask questions by PM, it hides answers from others.
    Quote Originally Posted by thefatladysingsopera
    the big numbers like 325,251 shouldn't they be the geometry.Secondly,how can i put my qtableview inside the layout and does that also apply for all other widgets in the application i am building i.e forms?.
    No, the row and column are the indexes of cells in the grid layout. So, if you want the space occupied by the grid to be divided into 3 rows of 5 cells you use row numbers from 0 to 2 and column from 0 to 4 to place widgets in the cells. The layout worries about where these cell boundaries map to on the available screen space and sizes the contained widget accordingly. Read Layout Management and QGridLayout.

    You put the table view into the layout exactly the same way you have put all the other widgets into the layout.

  6. The following user says thank you to ChrisW67 for this useful post:

    thefatladysingsopera (28th July 2011)

  7. #6
    Join Date
    Jun 2011
    Location
    Chennai, India
    Posts
    30
    Thanks
    13
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to display my application gui

    Regarding the crashing:
    Try renaming the variable smith in main.cpp to some other name. Using the class name causes crashes at times.

  8. #7
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to display my application gui

    The program did run eventually.I was following the address book example before.The books application example worked for me.

    Thanks.

Similar Threads

  1. OSD (On Screen Display) application on Qt Embedded
    By sundar.subramaniyan in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 17th July 2011, 18:32
  2. Launching GUI application on different X11 display
    By basy in forum Installation and Deployment
    Replies: 3
    Last Post: 3rd December 2010, 21:21
  3. Replies: 6
    Last Post: 19th September 2010, 06:42
  4. Replies: 0
    Last Post: 14th April 2010, 13:21
  5. Application doesn't display images
    By satoshi in forum Qt Programming
    Replies: 3
    Last Post: 2nd January 2010, 12:33

Tags for this Thread

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.