Results 1 to 4 of 4

Thread: Displaying the result of SQL queries in tabular form in window without using .ui file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Displaying the result of SQL queries in tabular form in window without using .ui

    You need to make your QTableView and QLabel part of your MainWindow. Simply creating them as standalone widgets won't work.

    The rest of your code in your addEntry() method is completely messed up. I won't even try to fix it - it looks like you have no good idea what to do and are trying anything you can to get something to work. Why don't you study some of the Qt SQL examples. There are a lot of them, from basic to more advanced.

    Meanwhile, here is one way to create your MainWindow (untested code, might have bugs):

    Qt Code:
    1. MainWindow::MainWindow( QWidget * pParent )
    2. : QMainWindow( pParent )
    3. {
    4. QLabel *labelExp = new QLabel(this);
    5. labelExp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    6. labelExp->setText("first line\nsecond line");
    7. labelExp->setAlignment(Qt::AlignBottom | Qt::AlignRight);
    8.  
    9. QTableView *view = new QTableView( this );
    10.  
    11. QWidget * pCentralWidget = new QWidget( this );
    12. QVBoxLayout * pLayout = new QVBoxLayout;
    13.  
    14. pLayout->addWidget( labelExp );
    15. pLayout->addWidget( view );
    16. pCentralWidget->setLayout( pLayout );
    17.  
    18. setCentralWidget( pCentralWidget );
    19.  
    20. // mpModel is a member variable of MainWindow
    21. mpModel1 = new QSqlQueryModel;
    22. mpModel1->setHeaderData(0, Qt::Horizontal, QObject::tr("Time"));
    23. mpModel1->setHeaderData(1, Qt::Horizontal, QObject::tr("Temperature"));
    24.  
    25. view->setModel( mpModel1 );
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 23rd November 2021 at 16:47.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    swati777999 (23rd November 2021)

Similar Threads

  1. Replies: 7
    Last Post: 1st December 2017, 15:27
  2. Replies: 1
    Last Post: 18th October 2011, 11:02
  3. present data in tabular form reading from a file
    By sachinmcajnu in forum Qt Programming
    Replies: 1
    Last Post: 2nd April 2011, 14:53
  4. Replies: 9
    Last Post: 19th November 2009, 09:18
  5. Replies: 1
    Last Post: 19th August 2007, 19:50

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
  •  
Qt is a trademark of The Qt Company.