PDA

View Full Version : QTableWidget along with QStackedWidget



smiling
12th March 2010, 20:27
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

toutarrive
13th March 2010, 08:15
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 .

smiling
13th March 2010, 11:52
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




# include <QtGui>
#--- more includes

class MyApplicationublic QMainWindow
{
Q_OBJECT

public:
myApplication();
~myApplication();
void createActions();
void createMenus();
void createToolBars();
void createStatusBar();
void addItems();
void addPage1Tabitems();
void addPage2items();
void addPage3items();
---
void addPageNitems();
-----
------
some more function related to application

QMenu *menu1;
----

QStackedWidget *mainWindow;

---
QToolBar *tollbar1;
----
QWidget *Page1;
QWidget *Page2;
--
QWidget *PageN;
-- etc

QTabWidget *page1TabWidget;

-- other variable & objects

public slots:
void Page1Tab1func1();
void Page1Tab1func2();

---
---

void Page1Tab2func1();
void Page1Tab2func2();
---
--
void Page2func1();
void Page2func2();


---
void Page3func1();
void Page3func2();



void PageNfunc1();
void PageNfunc2();
void PageNfuncM();

}



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



#include <QtGui>
#include "application.h"
--
--
#include ..

MyApplication::myApplication()
{
---
createActions();
createMenus();
createToolBars();
createStatusBar();
addItems();

---
----
}

void MyApplication::createActions()
{

---
}

void MyApplication::createMenus()
{

---
}

void MyApplication:: createToolBars()
{
// used to creating the toolbar
----some code

}

void MyApplication:: createStatusBar()
{
// status bar
------some code

}

void MyApplication::addItems()
{

//add all the item in my application to main app

mainWidow= new QStackedWidget; // Main Widget
setCentralWidget(mainWidow); // Central Widget

addPage1Tabitems(); // ???

addPage2items();
addPage3items();
---
addPageNitems();
--
}

//Help needed for tab widgets containing table widgets in different tabs
Void MyApplication:: addPage1Tabitems()
{
// I need help with this function .. sample code may help greatly
// plz point out where I am going wrong!!
// I am not using Designer

//I tried the following

page1MainWidget=new QWidget; // its added to the central stacked widget , contains the tab widget
QLabel * page1MainWidget Title= new QLabel ("<center><font size=\"4\">"+tr("Some Name ")+":</font></center>");

page1TabWidget = new QTabWidget;


page1Tab1=new QWidget;
QLabel * page1Tab1Title= new QLabel ("<center><font size=\"4\">"+tr("Pag1 Tab1 ")+":</font></center>");

tab1Table=new QTableWidget;
tab1Table ->setRowCount(9);
tab1Table ->setColumnCount(7);
tab1Table ->setParent(this);
//table -> verticalHeader()->hide();
tab1Table -> setSortingEnabled(false);
tab1Table -> setItemDelegate(&tab1Table_Delegate); //delegate define in separate files

-- similar code and layout as in addPage2item () tables .. see next function below
tab1Table ->setLayout(layoutTab1Table);

// page1Tab1Widget ->addWidget(tab1Table); // getting error , what to do??


-- otherwise define layout for page1Tab2Widget.. dot know if that’s how it should be??

-- page1Tab1WidgetLayout = new QHBoxLayout;
-- if addLayout need to be used here??
-- page1Tab1WidgetLayout-> addWidget(tab1Table); //??

-- page1Tab1-> setLayout (page1Tab1WidgetLayout); //??

-- page1TabWidget -> addTab(page1Tab1,tr("Page1Tab1 "));


//------------------------------------------------------------------------
//Tab 2 of the Tab widget in Page 1
//------------------------------------------------------------------------
page1Tab2 = new QWidget;
page1Tab1Label = new QLabel("<font size=\"5\">" "Some Name</font>");


-- similar code as above for table & layout
Tab2Table=new QTableWidget;
---
---
tab2Table ->setLayout(layoutTab2Table);
//page1Tab2Widget ->addWidget(tab2Table); // getting error , what to do??

--otherwise define layout for page1Tab2Widget

-- page1Tab2WidgetLayout = new QHBoxLayout;
-- if addLayout need to be used here??
-- page1Tab2WidgetLayout-> addWidget(tab1Table); //??

-- page1Tab2-> setLayout (page1Tab2WidgetLayout); //??

-- page1TabWidget -> addTab(page1Tab2,tr("Page1Tab2 "));

//-----------------------------------------
// Similarly for other tabs
/------------------------------------------

//Finally stitching the tab widget to the central stacked widget
horizontalLayout = new QHBoxLayout;
horizontalLayout->addWidget(page1TabWidget);

page1MainWidget ->setLayout(horizontalLayout);
mainWidow->addWidget(page1MainWidget);
}


// 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()]

Void MyApplication :: addPage2Items()
{
appPage2=new QWidget;
QLabel * appPage2Title= new QLabel ("<center><font size=\"4\">"+tr("Some Name")+":</font></center>");

appTablePage2=new QTableWidget;
appTablePage2->setRowCount(9);
appTablePage2->setColumnCount(7);
appTablePage2->setParent(this);
//table -> verticalHeader()->hide();
appTablePage2-> setSortingEnabled(true);//old false
appTablePage2-> setItemDelegate(&appTablePage2_Delegate); //suitable delegate defined in
//separate files

appTablePage2labels << tr("Label1") <<tr("Label2") << tr("Label3") << tr("Label14") << tr("Label5")<<tr("La6el1")<<tr("Label7); // lables/column can be added or deleted

universalAppTablePage2labels<<"Lable1"<<"Lable2"<<"Lable2 Lable3"<<"Lable4"<<"Lable5"<<"Lable6"<<"Lable7";
// Setting lables and column width
appTablePage2->setHorizontalHeaderLabels(appTablePage2labels);
for(int i=0;i<2;i++)
{
appTablePage2 -> setColumnWidth(i,100);
};
for(int i=2;i<= appTablePage2->columnCount()-1;i++)
{
appTablePage2 -> setColumnWidth(i,960/( appTablePage2->columnCount()-1));
}
appTablePage2 -> horizontalHeader() -> setStretchLastSection(true);

//Add push buttons for adding columns and rows to table

newABRow = new QPushButton(tr("Add Row"));
newABCmn = new QPushButton(tr("Add Column"));
delABCmn = new QPushButton(tr("Delete Column"));
delABRow = new QPushButton(tr("Delete Row"));
newABRow -> setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixe d);
newABCmn -> setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixe d);
delABRow-> setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixe d);
delABCmn-> setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixe d);

//make connections to the slots
connect(newABRow,SIGNAL(clicked()),this,SLOT(newRo wAB()));
connect(delABRow,SIGNAL(clicked()),this,SLOT(delRo wAB()));
connect(newABCmn,SIGNAL(clicked()),this,SLOT(newCo lumnAB()));
connect(delABCmn,SIGNAL(clicked()),this,SLOT(delCo lumnAB()));
connect( appPage2,SIGNAL(focusLost()),this,SLOT(saveAppPage 2()));

//Layouts:

abVl=new QVBoxLayout;
abVl->addWidget(appPage2Title);
abVl->addWidget(appTablePage2);
abVl -> setSizeConstraint(QLayout::SetNoConstraint);

abLabel = new QLabel(tr("Some Name"));
abCombo = new QComboBox;

--some code for abCombo and Layout

abLayout = new QHBoxLayout;
abLayout -> addWidget(abLabel);
abLayout -> addWidget(abCombo);
abLayout -> setAlignment(Qt::AlignRight);

abCenterLayout = new QHBoxLayout;
abCenterLayout->addWidget(newABCmn);
abCenterLayout->addSpacing(50);
abCenterLayout->addWidget(delABCmn);
abCenterLayout->addSpacing(50);
abCenterLayout->addWidget(newABRow);
abCenterLayout->addSpacing(50);
abCenterLayout->addWidget(delABRow);
abCenterLayout-> setAlignment(Qt::AlignHCenter);

abBHl=new QHBoxLayout;
abBHl->addLayout(abCenterLayout);
abBHl->addSpacing(50);
abBHl->addLayout(abLayout);
abBHl-> setAlignment(Qt::AlignHCenter);
abVl->addLayout(abBHl);

QHBoxLayout *layoutAB= new QHBoxLayout;
layoutAB->addSpacing(10);
layoutAB->addLayout(abVl);
layoutAB->addSpacing(10);

appPage2->setLayout(layoutAB);
mainWidow->addWidget(appPage2);

};





void MyApplication::ChangetoPage2()
{
---some code
mainWidow->setCurrentIndex(1);
};

void MyApplication::ChangetoPage3()
{
---some code
mainWidow->setCurrentIndex(2);
};

----
----
void MyApplication::ChangetoPageN()
{
---some code
mainWidow->setCurrentIndex(N-1);
};

---similarly for other pages for changing the index on action

--some more code

--slot codes ..

toutarrive
13th March 2010, 12:03
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.

smiling
13th March 2010, 12:27
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


# include <QtGui>
#--- more includes

class MyApplication:public QMainWindow
{
Q_OBJECT

public:
myApplication();
~myApplication();
void createActions();
void createMenus();
void createToolBars();
void createStatusBar();
void addItems();
void addPage1Tabitems();
void addPage2items();
void addPage3items();
---
void addPageNitems();
-----
------
some more function related to application

QMenu *menu1;
----

QStackedWidget *mainWindow;

---
QToolBar *tollbar1;
----
QWidget *Page1;
QWidget *Page2;
--
QWidget *PageN;
-- etc

QTabWidget *page1TabWidget;

-- other variable & objects

public slots:
void Page1Tab1func1();
void Page1Tab1func2();

---
---

void Page1Tab2func1();
void Page1Tab2func2();
---
--
void Page2func1();
void Page2func2();


---
void Page3func1();
void Page3func2();



void PageNfunc1();
void PageNfunc2();
void PageNfuncM();

}


-------------------------------------------------------------------------------------------------------------------------------
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.

toutarrive
13th March 2010, 12:46
The add item function are not in slots


public slots:
void addPage1Tab1items();
void addPage1Tab2items();
void addPage2items();
void addPage3items();

void addPageNitems();

If your don't mind before coming with other implementation , try to put into practise the advice given.