Results 1 to 4 of 4

Thread: Question : place TableView in a tab

  1. #1
    Join Date
    May 2012
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows
    Thanks
    1

    Default Question : place TableView in a tab

    Hi all,

    I am freshman of Qt programming. After setup the develop enviroment, I'm trying to layout something by pure coding but without Qt Designer in case to be familiar with Qt Libraries.

    And now, I'm trying to place a table into a tab.

    I can do this by a QTableWidget and QVBoxLayout, But it doesn't work by QTableView and QVBoxLayout....

    I had tried for few days, but I still can't figure this out. So I want to raise up this question, is there any solution???

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

    Default Re: Question : place TableView in a tab

    "But it doesn't work," is not a useful description of a problem especially if you expect a useful answer to "is there any solution???". The answer is Yes.

    Is there any chance you could show us what you have actually done? What, if any, are your compiler/linker/runtime error/warning messages? What is the on-screen result? What were you expecting? See this for help.

    A QTableWidget is-a QTableView, and both are QWidgets, so the two are interchangeable as far as layouts are concerned.

  3. #3
    Join Date
    May 2012
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows
    Thanks
    1

    Default Re: Question : place TableView in a tab

    After try and error in this morning, now the tableview can successfully display in the Tab.
    But application output section came some messages :

    Qt Code:
    1. QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
    2. QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
    3. QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
    4. QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
    5. QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
    6. QModelIndex(0,0,0x0,QAbstractTableModel(0xb4f070) )
    7. ...going on...
    To copy to clipboard, switch view to plain text mode 

    Here is the code. using Qt Creator IDE.
    I made several tabs and try to place a table in the Tab named "TestTbTab"
    In m_TabDialog.cpp, comment part is using TableWidget and can successfully build.
    and the rest of the part shows my TableView usage.


    m_TabDialog.h
    Qt Code:
    1. #ifndef M_TABDIALOG_H
    2. #define M_TABDIALOG_H
    3.  
    4. #include<QtGui>
    5. #include<QDialog>
    6. #include<QTableView>
    7. #include<QTableWidget>
    8. #include<QTableWidgetItem>
    9. #include<QGridLayout>
    10. #include"TableModel.h"
    11.  
    12. QT_BEGIN_NAMESPACE
    13. class QFileInfo;
    14. class QTabWidget;
    15. QT_END_NAMESPACE
    16.  
    17. // class m a i n t a b
    18. class MainTab : public QWidget
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. MainTab(const QFileInfo &fileInfo, QWidget *parent = 0);
    24. };
    25.  
    26. // class D E B U G T A B
    27. class DebugTab : public QWidget
    28. {
    29. Q_OBJECT
    30.  
    31. public:
    32. DebugTab(const QFileInfo &fileInfo, QWidget *parent =0);
    33. };
    34.  
    35. // class T e s t T a b
    36. class TestTab : public QWidget
    37. {
    38. Q_OBJECT
    39.  
    40. public:
    41. TestTab(const QFileInfo &fileInfo, QWidget *parent = 0);
    42. };
    43.  
    44. //class T e s t T b T a b
    45. class TestTbTab : public QWidget
    46. {
    47. public:
    48. TestTbTab(const QFileInfo &fileInfo, QWidget *parent = 0);
    49. QWidget *tab;
    50. private:
    51. QGridLayout *grid_layout;
    52. TableModel *model;
    53. QTableView *view;
    54.  
    55. };
    56.  
    57. //class T a b D i a l o g
    58. class TabDialog : public QDialog
    59. {
    60. Q_OBJECT
    61.  
    62. public:
    63. TabDialog(const QString &fileName, QWidget *parent = 0);
    64.  
    65. private:
    66. QTabWidget *tabWidget;
    67. QWidget tab;
    68. };
    69.  
    70.  
    71. #endif // M_TABDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    m_TabDialog.cpp (for focusing on problem, pass other Tabs)

    Qt Code:
    1. #include"m_TabDialog.h"
    2.  
    3. TabDialog::TabDialog(const QString &fileName, QWidget *parent) : QDialog(parent)
    4. {
    5. QFileInfo fileInfo(fileName);
    6.  
    7. tabWidget = new QTabWidget;
    8. tabWidget->addTab(new MainTab(fileInfo),tr("Main"));
    9. tabWidget->addTab(new DebugTab(fileInfo),tr("Debug"));
    10. tabWidget->addTab(new TestTab(fileInfo),tr("Test"));
    11. tabWidget->addTab(new TestTbTab(fileInfo),tr("testTable"));
    12.  
    13. btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
    14.  
    15. connect(btnBox, SIGNAL(accepted()),this,SLOT(accept()));
    16. connect(btnBox, SIGNAL(accepted()),this,SLOT(reject()));
    17.  
    18. QVBoxLayout *mainLayout = new QVBoxLayout;
    19. mainLayout->addWidget(tabWidget);
    20. mainLayout->addWidget(btnBox);
    21. setLayout(mainLayout);
    22.  
    23. setWindowTitle(tr("test yaya"));
    24. }
    25. /*
    26. TestTbTab::TestTbTab(const QFileInfo &fileInfo, QWidget *parent) : QWidget(parent)
    27. {
    28.   QTableWidget *tableWidget = new QTableWidget;
    29.   tableWidget->setRowCount(5);
    30.   tableWidget->setColumnCount(5);
    31.  
    32.   tableWidget->setWindowTitle(QObject::tr("test tablewidget"));
    33.   tableWidget->maximumSize();
    34.  
    35.   QStringList m_header;
    36.   m_header<<"item1"<<"item2";
    37.   tableWidget->setHorizontalHeaderLabels(m_header);
    38.  
    39.   tableWidget->setItem(0,0,new QTableWidgetItem("Jan"));
    40.   tableWidget->setItem(1,0,new QTableWidgetItem("Feb"));
    41.   tableWidget->setItem(2,0,new QTableWidgetItem("Feb"));
    42.  
    43.   QVBoxLayout *layout = new QVBoxLayout;
    44.   layout->addWidget(tableWidget);
    45.   setLayout(layout);
    46. }*/
    47.  
    48. TestTbTab::TestTbTab(const QFileInfo &fileInfo, QWidget *parent) : QWidget(parent)
    49. {
    50. model = new TableModel(this);
    51.  
    52. grid_layout = new QGridLayout;
    53. tab = new QWidget;
    54. view = new QTableView(tab);
    55. view->setModel(model);
    56. view->setWindowTitle(QObject::tr("test designed Model"));
    57. grid_layout->addWidget(view);
    58. setLayout(grid_layout);
    59. }
    To copy to clipboard, switch view to plain text mode 

    thanks for any advise / suggestion

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

    Default Re: Question : place TableView in a tab

    Good to see you fixed the original issue with the QTableView.
    thanks for any advise / suggestion
    You haven't described a new problem so I am not sure what advice you want.

    Line 69 of your .h file, and line 53 of the .cpp file seem to be unneeded (member variable called tab) and will be a memory leak because you not allocate the object a parent at creation.

    The output you refer to is not coming from this code. Probably a debugging statement inside the TableModel class I would guess. Not indicative of an error of any sort.

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

    coolinstar (18th May 2012)

Similar Threads

  1. addWidget other place than setCentralWidget()
    By darkrptyp in forum Newbie
    Replies: 3
    Last Post: 18th November 2011, 19:35
  2. Quick Question:Why my TableView appears blank?!
    By fatecasino in forum Newbie
    Replies: 2
    Last Post: 4th March 2011, 12:06
  3. TableView/Model Display Question
    By SixDegrees in forum Qt Programming
    Replies: 9
    Last Post: 27th August 2010, 09:06
  4. How to place contextmenu in QGraphicsScene?
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2007, 16:04

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.