Results 1 to 3 of 3

Thread: Table isn't showed

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Table isn't showed

    Hello, Everyone

    I don't see the content of the table. Why?

    67.png

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QMenuBar>
    3. #include <QToolBar>
    4. #include <QIcon>
    5. #include <QAction>
    6. #include <QMenu>
    7. #include <QStatusBar>
    8. #include <QTableView>
    9. #include <QWidget>
    10. #include <QHBoxLayout>
    11. #include <QVBoxLayout>
    12. #include <QLabel>
    13. #include <QSplitter>
    14.  
    15. #include <QStandardItemModel>
    16.  
    17. MainWindow::MainWindow(QWidget *parent) :
    18. QMainWindow(parent)
    19. {
    20. createActions();
    21. createMenus();
    22. createToolBars();
    23. statusBar()->showMessage(tr(""));
    24.  
    25. QTableView *table = new QTableView;
    26.  
    27. QSplitter splitter;
    28. splitter.addWidget( table );
    29.  
    30. QStandardItemModel model(5, 2);
    31. for( int r=0; r<5; r++ )
    32. for( int c=0; c<2; c++)
    33. {
    34. QStandardItem *item = new QStandardItem( QString("Row:%0, Column:%1").arg(r).arg(c) );
    35.  
    36. if( c == 0 )
    37. for( int i=0; i<3; i++ )
    38. item->appendRow( new QStandardItem( QString("Item %0").arg(i) ) );
    39.  
    40. model.setItem(r, c, item);
    41. }
    42.  
    43. // model.setHorizontalHeaderItem( 0, new QStandardItem( "Name" ) );
    44. // model.setHorizontalHeaderItem( 1, new QStandardItem( "Phone number" ) );
    45.  
    46. table->setModel( &model );
    47. // table->setSelectionModel( table->selectionModel() );
    48.  
    49. QWidget *window = new QWidget;
    50. setCentralWidget(window);
    51.  
    52. QLabel *matrixLabel = new QLabel(tr("Matrix:"));
    53. QVBoxLayout *matrixLayout = new QVBoxLayout;
    54. matrixLayout->addWidget(matrixLabel);
    55. matrixLayout->addWidget(table);
    56.  
    57. QHBoxLayout *mainLayout = new QHBoxLayout;
    58. mainLayout->addLayout(matrixLayout);
    59.  
    60. window->setLayout(mainLayout);
    61. }
    62.  
    63. void MainWindow::createActions()
    64. {
    65. loadAction = new QAction(QIcon(":/images/load.png"), tr("&Open"), this);
    66. loadAction->setShortcut(tr("Ctrl+L"));
    67. loadAction->setStatusTip(tr("Load a matrix"));
    68. connect(loadAction, SIGNAL(triggered()), this, SLOT(loadSLOT()));
    69.  
    70. saveAction = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
    71. saveAction->setShortcut(tr("Ctrl+S"));
    72. saveAction->setStatusTip(tr("Save a matrix"));
    73. connect(saveAction, SIGNAL(triggered()), this, SLOT(saveSLOT()));
    74.  
    75. saveAsAction = new QAction(QIcon(":/images/save_as.png"), tr("&Save As"), this);
    76. saveAsAction->setShortcut(tr("Ctrl+Alt+S"));
    77. saveAsAction->setStatusTip(tr("Save a matrix As..."));
    78. connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAsSLOT()));
    79. }
    80.  
    81. void MainWindow::createMenus()
    82. {
    83. QMenu *menu;
    84.  
    85. menu = menuBar()->addMenu(tr("&File"));
    86. menu->addAction(loadAction);
    87. menu->addAction(saveAction);
    88. menu->addAction(saveAsAction);
    89. }
    90.  
    91. void MainWindow::createToolBars()
    92. {
    93. QToolBar *toolbar;
    94.  
    95. toolbar = addToolBar(tr("File"));
    96. toolbar->addAction(loadAction);
    97. toolbar->addAction(saveAction);
    98. toolbar->addAction(saveAsAction);
    99. }
    100.  
    101. void MainWindow::loadSLOT()
    102. {
    103.  
    104. }
    105.  
    106. void MainWindow::saveSLOT()
    107. {
    108.  
    109. }
    110.  
    111. void MainWindow::saveAsSLOT()
    112. {
    113.  
    114. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. #include <QAction>
    7.  
    8. class MainWindow : public QMainWindow
    9. {
    10. Q_OBJECT
    11. public:
    12. MainWindow(QWidget *parent = 0);
    13.  
    14. private slots:
    15. void loadSLOT();
    16. void saveSLOT();
    17. void saveAsSLOT();
    18.  
    19. private:
    20. void createActions();
    21. void createMenus();
    22. void createToolBars();
    23.  
    24. QAction *loadAction;
    25. QAction *saveAction;
    26. QAction *saveAsAction;
    27. };
    28.  
    29. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. MainWindow mw;
    9. mw.resize(500, 300);
    10. mw.show();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    MaxElement.pro
    Qt Code:
    1. SOURCES += \
    2. main.cpp \
    3. mainwindow.cpp
    4.  
    5. HEADERS += \
    6. mainwindow.h
    7.  
    8. RESOURCES += \
    9. images.qrc
    To copy to clipboard, switch view to plain text mode 

    Thanks

  2. #2
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Table isn't showed

    Change line 30 of mainwindow.cpp to
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    and the other lines accordingly.

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

    8Observer8 (5th March 2013)

  4. #3
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Table isn't showed

    Thanks very much! You saved my life

    Qt Code:
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. model->setItem(r, c, item);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. table->setModel(model);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 2nd November 2011, 11:13
  2. Replies: 4
    Last Post: 5th May 2011, 10:26
  3. Can i get index list that showed in viewport?
    By jerry7 in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2010, 20:58
  4. Replies: 2
    Last Post: 12th February 2009, 09:53
  5. No Background image showed
    By jiveaxe in forum Installation and Deployment
    Replies: 2
    Last Post: 30th December 2007, 13:25

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.