Results 1 to 6 of 6

Thread: QTableWidget along with QStackedWidget

  1. #1
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QTableWidget along with QStackedWidget

    Hi,
    I am trying to develop and application that uses QStackedWidget and with the help of the toolbar the transition from one page to the other is achieved. Precisely, for the interface a central QStackedWidget object named mainWidow is used and the change from the currently active page [indicated by an icon in the toolbar] is achieved with the help of the toolbar. In one of the page [indicated by an icon in the toolbar] i want to add a tab widget/ tab bar. Each tab will contain a table QTableWidget.

    I want to know how to do that and if anyone can share some example/skeleton codes. I have been able to make the QTWidget consiting of QTableWidget as a part of the mainWidow working, however when i am clueless on including these tables in a tab widget [QTabWidget] and the tab widget in turn as part of the central QStackedWidget controlled by the toolbar.


    Any help is greatly appreciated..
    Cheers

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget along with QStackedWidget

    Do your layout using Qt designer and have a look at the code generated automatically.
    It should give you a good starting point if you want to code it yourself .

  3. #3
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget along with QStackedWidget

    Thanks for responding. I am not using designer. Plz share your experience and let me know where i am going wrong and if the approach i am adopting is correct. My application skeleton looks somewhat like this

    //-------------------------------------------------------------------------------------------------------------------------
    application.h // header file

    Qt Code:
    1. # include <QtGui>
    2. #--- more includes
    3.  
    4. class MyApplicationublic QMainWindow
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. myApplication();
    10. ~myApplication();
    11. void createActions();
    12. void createMenus();
    13. void createToolBars();
    14. void createStatusBar();
    15. void addItems();
    16. void addPage1Tabitems();
    17. void addPage2items();
    18. void addPage3items();
    19. ---
    20. void addPageNitems();
    21. -----
    22. ------
    23. some more function related to application
    24.  
    25. QMenu *menu1;
    26. ----
    27.  
    28. QStackedWidget *mainWindow;
    29.  
    30. ---
    31. QToolBar *tollbar1;
    32. ----
    33. QWidget *Page1;
    34. QWidget *Page2;
    35. --
    36. QWidget *PageN;
    37. -- etc
    38.  
    39. QTabWidget *page1TabWidget;
    40.  
    41. -- other variable & objects
    42.  
    43. public slots:
    44. void Page1Tab1func1();
    45. void Page1Tab1func2();
    46.  
    47. ---
    48. ---
    49.  
    50. void Page1Tab2func1();
    51. void Page1Tab2func2();
    52. ---
    53. --
    54. void Page2func1();
    55. void Page2func2();
    56.  
    57.  
    58. ---
    59. void Page3func1();
    60. void Page3func2();
    61.  
    62.  
    63.  
    64. void PageNfunc1();
    65. void PageNfunc2();
    66. void PageNfuncM();
    67.  
    68. }
    To copy to clipboard, switch view to plain text mode 


    -------------------------------------------------------------------------------------------------------------------------------
    application.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "application.h"
    3. --
    4. --
    5. #include ..
    6.  
    7. MyApplication::myApplication()
    8. {
    9. ---
    10. createActions();
    11. createMenus();
    12. createToolBars();
    13. createStatusBar();
    14. addItems();
    15.  
    16. ---
    17. ----
    18. }
    19.  
    20. void MyApplication::createActions()
    21. {
    22.  
    23. ---
    24. }
    25.  
    26. void MyApplication::createMenus()
    27. {
    28.  
    29. ---
    30. }
    31.  
    32. void MyApplication:: createToolBars()
    33. {
    34. // used to creating the toolbar
    35. ----some code
    36.  
    37. }
    38.  
    39. void MyApplication:: createStatusBar()
    40. {
    41. // status bar
    42. ------some code
    43.  
    44. }
    45.  
    46. void MyApplication::addItems()
    47. {
    48.  
    49. //add all the item in my application to main app
    50.  
    51. mainWidow= new QStackedWidget; // Main Widget
    52. setCentralWidget(mainWidow); // Central Widget
    53.  
    54. addPage1Tabitems(); // ???
    55.  
    56. addPage2items();
    57. addPage3items();
    58. ---
    59. addPageNitems();
    60. --
    61. }
    62.  
    63. //Help needed for tab widgets containing table widgets in different tabs
    64. Void MyApplication:: addPage1Tabitems()
    65. {
    66. // I need help with this function .. sample code may help greatly
    67. // plz point out where I am going wrong!!
    68. // I am not using Designer
    69.  
    70. //I tried the following
    71.  
    72. page1MainWidget=new QWidget; // its added to the central stacked widget , contains the tab widget
    73. QLabel * page1MainWidget Title= new QLabel ("<center><font size=\"4\">"+tr("Some Name ")+":</font></center>");
    74.  
    75. page1TabWidget = new QTabWidget;
    76.  
    77.  
    78. page1Tab1=new QWidget;
    79. QLabel * page1Tab1Title= new QLabel ("<center><font size=\"4\">"+tr("Pag1 Tab1 ")+":</font></center>");
    80.  
    81. tab1Table=new QTableWidget;
    82. tab1Table ->setRowCount(9);
    83. tab1Table ->setColumnCount(7);
    84. tab1Table ->setParent(this);
    85. //table -> verticalHeader()->hide();
    86. tab1Table -> setSortingEnabled(false);
    87. tab1Table -> setItemDelegate(&tab1Table_Delegate); //delegate define in separate files
    88.  
    89. -- similar code and layout as in addPage2item () tables .. see next function below
    90. tab1Table ->setLayout(layoutTab1Table);
    91.  
    92. // page1Tab1Widget ->addWidget(tab1Table); // getting error , what to do??
    93.  
    94.  
    95. -- otherwise define layout for page1Tab2Widget.. dot know if that’s how it should be??
    96.  
    97. -- page1Tab1WidgetLayout = new QHBoxLayout;
    98. -- if addLayout need to be used here??
    99. -- page1Tab1WidgetLayout-> addWidget(tab1Table); //??
    100.  
    101. -- page1Tab1-> setLayout (page1Tab1WidgetLayout); //??
    102.  
    103. -- page1TabWidget -> addTab(page1Tab1,tr("Page1Tab1 "));
    104.  
    105.  
    106. //------------------------------------------------------------------------
    107. //Tab 2 of the Tab widget in Page 1
    108. //------------------------------------------------------------------------
    109. page1Tab2 = new QWidget;
    110. page1Tab1Label = new QLabel("<font size=\"5\">" "Some Name</font>");
    111.  
    112.  
    113. -- similar code as above for table & layout
    114. Tab2Table=new QTableWidget;
    115. ---
    116. ---
    117. tab2Table ->setLayout(layoutTab2Table);
    118. //page1Tab2Widget ->addWidget(tab2Table); // getting error , what to do??
    119.  
    120. --otherwise define layout for page1Tab2Widget
    121.  
    122. -- page1Tab2WidgetLayout = new QHBoxLayout;
    123. -- if addLayout need to be used here??
    124. -- page1Tab2WidgetLayout-> addWidget(tab1Table); //??
    125.  
    126. -- page1Tab2-> setLayout (page1Tab2WidgetLayout); //??
    127.  
    128. -- page1TabWidget -> addTab(page1Tab2,tr("Page1Tab2 "));
    129.  
    130. //-----------------------------------------
    131. // Similarly for other tabs
    132. /------------------------------------------
    133.  
    134. //Finally stitching the tab widget to the central stacked widget
    135. horizontalLayout = new QHBoxLayout;
    136. horizontalLayout->addWidget(page1TabWidget);
    137.  
    138. page1MainWidget ->setLayout(horizontalLayout);
    139. mainWidow->addWidget(page1MainWidget);
    140. }
    141.  
    142.  
    143. // This table widget in page 2 of the stacked widget works , I want to add same/similar multiple table widgets in tabs in Page1 [addPage1Tabitems()]
    144.  
    145. Void MyApplication :: addPage2Items()
    146. {
    147. appPage2=new QWidget;
    148. QLabel * appPage2Title= new QLabel ("<center><font size=\"4\">"+tr("Some Name")+":</font></center>");
    149.  
    150. appTablePage2=new QTableWidget;
    151. appTablePage2->setRowCount(9);
    152. appTablePage2->setColumnCount(7);
    153. appTablePage2->setParent(this);
    154. //table -> verticalHeader()->hide();
    155. appTablePage2-> setSortingEnabled(true);//old false
    156. appTablePage2-> setItemDelegate(&appTablePage2_Delegate); //suitable delegate defined in
    157. //separate files
    158.  
    159. appTablePage2labels << tr("Label1") <<tr("Label2") << tr("Label3") << tr("Label14") << tr("Label5")<<tr("La6el1")<<tr("Label7); // lables/column can be added or deleted
    160.  
    161. universalAppTablePage2labels<<"Lable1"<<"Lable2"<<"Lable2 Lable3"<<"Lable4"<<"Lable5"<<"Lable6"<<"Lable7";
    162. // Setting lables and column width
    163. appTablePage2->setHorizontalHeaderLabels(appTablePage2labels);
    164. for(int i=0;i<2;i++)
    165. {
    166. appTablePage2 -> setColumnWidth(i,100);
    167. };
    168. for(int i=2;i<= appTablePage2->columnCount()-1;i++)
    169. {
    170. appTablePage2 -> setColumnWidth(i,960/( appTablePage2->columnCount()-1));
    171. }
    172. appTablePage2 -> horizontalHeader() -> setStretchLastSection(true);
    173.  
    174. //Add push buttons for adding columns and rows to table
    175.  
    176. newABRow = new QPushButton(tr("Add Row"));
    177. newABCmn = new QPushButton(tr("Add Column"));
    178. delABCmn = new QPushButton(tr("Delete Column"));
    179. delABRow = new QPushButton(tr("Delete Row"));
    180. newABRow -> setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    181. newABCmn -> setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    182. delABRow-> setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    183. delABCmn-> setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    184.  
    185. //make connections to the slots
    186. connect(newABRow,SIGNAL(clicked()),this,SLOT(newRowAB()));
    187. connect(delABRow,SIGNAL(clicked()),this,SLOT(delRowAB()));
    188. connect(newABCmn,SIGNAL(clicked()),this,SLOT(newColumnAB()));
    189. connect(delABCmn,SIGNAL(clicked()),this,SLOT(delColumnAB()));
    190. connect( appPage2,SIGNAL(focusLost()),this,SLOT(saveAppPage2()));
    191.  
    192. //Layouts:
    193.  
    194. abVl=new QVBoxLayout;
    195. abVl->addWidget(appPage2Title);
    196. abVl->addWidget(appTablePage2);
    197. abVl -> setSizeConstraint(QLayout::SetNoConstraint);
    198.  
    199. abLabel = new QLabel(tr("Some Name"));
    200. abCombo = new QComboBox;
    201.  
    202. --some code for abCombo and Layout
    203.  
    204. abLayout = new QHBoxLayout;
    205. abLayout -> addWidget(abLabel);
    206. abLayout -> addWidget(abCombo);
    207. abLayout -> setAlignment(Qt::AlignRight);
    208.  
    209. abCenterLayout = new QHBoxLayout;
    210. abCenterLayout->addWidget(newABCmn);
    211. abCenterLayout->addSpacing(50);
    212. abCenterLayout->addWidget(delABCmn);
    213. abCenterLayout->addSpacing(50);
    214. abCenterLayout->addWidget(newABRow);
    215. abCenterLayout->addSpacing(50);
    216. abCenterLayout->addWidget(delABRow);
    217. abCenterLayout-> setAlignment(Qt::AlignHCenter);
    218.  
    219. abBHl=new QHBoxLayout;
    220. abBHl->addLayout(abCenterLayout);
    221. abBHl->addSpacing(50);
    222. abBHl->addLayout(abLayout);
    223. abBHl-> setAlignment(Qt::AlignHCenter);
    224. abVl->addLayout(abBHl);
    225.  
    226. QHBoxLayout *layoutAB= new QHBoxLayout;
    227. layoutAB->addSpacing(10);
    228. layoutAB->addLayout(abVl);
    229. layoutAB->addSpacing(10);
    230.  
    231. appPage2->setLayout(layoutAB);
    232. mainWidow->addWidget(appPage2);
    233.  
    234. };
    235.  
    236.  
    237.  
    238.  
    239.  
    240. void MyApplication::ChangetoPage2()
    241. {
    242. ---some code
    243. mainWidow->setCurrentIndex(1);
    244. };
    245.  
    246. void MyApplication::ChangetoPage3()
    247. {
    248. ---some code
    249. mainWidow->setCurrentIndex(2);
    250. };
    251.  
    252. ----
    253. ----
    254. void MyApplication::ChangetoPageN()
    255. {
    256. ---some code
    257. mainWidow->setCurrentIndex(N-1);
    258. };
    259.  
    260. ---similarly for other pages for changing the index on action
    261.  
    262. --some more code
    263.  
    264. --slot codes ..
    To copy to clipboard, switch view to plain text mode 
    Last edited by smiling; 13th March 2010 at 13:04.

  4. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget along with QStackedWidget

    You are using a wrong and far too complicated approach. No need to use slot to add widgets to your app.
    Read Qt doc and go through examples to get a good grasp about layouts and creating widgets.
    Once more, for learning purpose, implement your design trough Qt designer and view the code generated.
    It will gives you the rigth way to create widgets within a form.

    Besides, visit this link (http://www.qtcentre.org/misc.php?do=bbcode) about posting rules.
    Last edited by toutarrive; 13th March 2010 at 12:10.

  5. #5
    Join Date
    Mar 2010
    Posts
    3
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget along with QStackedWidget

    Some correction on the application.h
    The add item function are not in slots .. slots are not used to add widgets to the app. The structure i am using is as follows

    //-------------------------------------------------------------------------------------------------------------------------
    application.h // header file
    Qt Code:
    1. # include <QtGui>
    2. #--- more includes
    3.  
    4. class MyApplication:public QMainWindow
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. myApplication();
    10. ~myApplication();
    11. void createActions();
    12. void createMenus();
    13. void createToolBars();
    14. void createStatusBar();
    15. void addItems();
    16. void addPage1Tabitems();
    17. void addPage2items();
    18. void addPage3items();
    19. ---
    20. void addPageNitems();
    21. -----
    22. ------
    23. some more function related to application
    24.  
    25. QMenu *menu1;
    26. ----
    27.  
    28. QStackedWidget *mainWindow;
    29.  
    30. ---
    31. QToolBar *tollbar1;
    32. ----
    33. QWidget *Page1;
    34. QWidget *Page2;
    35. --
    36. QWidget *PageN;
    37. -- etc
    38.  
    39. QTabWidget *page1TabWidget;
    40.  
    41. -- other variable & objects
    42.  
    43. public slots:
    44. void Page1Tab1func1();
    45. void Page1Tab1func2();
    46.  
    47. ---
    48. ---
    49.  
    50. void Page1Tab2func1();
    51. void Page1Tab2func2();
    52. ---
    53. --
    54. void Page2func1();
    55. void Page2func2();
    56.  
    57.  
    58. ---
    59. void Page3func1();
    60. void Page3func2();
    61.  
    62.  
    63.  
    64. void PageNfunc1();
    65. void PageNfunc2();
    66. void PageNfuncM();
    67.  
    68. }
    To copy to clipboard, switch view to plain text mode 

    -------------------------------------------------------------------------------------------------------------------------------
    i understand that it will be test of patience and persistence.. using the approach. However, it seemed to be the inclined approach taken by the experienced developers in the development of this app earlier.
    Last edited by smiling; 13th March 2010 at 13:18.

  6. #6
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget along with QStackedWidget

    The add item function are not in slots
    Qt Code:
    1. public slots:
    2. void addPage1Tab1items();
    3. void addPage1Tab2items();
    4. void addPage2items();
    5. void addPage3items();
    6.  
    7. void addPageNitems();
    To copy to clipboard, switch view to plain text mode 
    If your don't mind before coming with other implementation , try to put into practise the advice given.

Similar Threads

  1. Help with QStackedWidget
    By onírico in forum Newbie
    Replies: 6
    Last Post: 12th November 2009, 16:34
  2. QStackedWidget scrollable
    By mattia in forum Qt Tools
    Replies: 1
    Last Post: 19th April 2008, 09:02
  3. QStackedWidget question
    By hgedek in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2007, 16:36
  4. QSplitter and QStackedWidget
    By nikita in forum Qt Programming
    Replies: 4
    Last Post: 15th November 2006, 04:52
  5. parsing QtableWidget from a QStackedWidget
    By rosmarcha in forum Qt Programming
    Replies: 10
    Last Post: 13th October 2006, 14:36

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.