August 3, 2011

alfah alfah
Lab Rat
157 posts

[Solved]creating tabs

Page  
1

hi

Im new to creating widgets programmatically. I got the application running but no tabs were created

This is my code

  1. Tabs::Tabs(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::Tabs)
  4. {
  5.     ui->setupUi(this);
  6.     tabWidget = new QTabWidget;
  7.     tabWidget->addTab(new QWidget,tr("History"));
  8.     tabWidget->addTab(new QWidget,tr("Calender"));
  9.     tabWidget->addTab(new QWidget,tr("Statistics"))    ;
  10.     mainLayout = new QVBoxLayout;
  11.     mainLayout->addWidget(tabWidget);
  12.     setLayout(mainLayout);
  13.  
  14. }

and in the header file
I’ve declared the following
  1.    QTabWidget *tabWidget;
  2.    QVBoxLayout *mainLayout;

alfah

47 replies

August 3, 2011

cincirin cincirin
Ant Farmer
387 posts

Include the header where HistoryTab and others are declared.

August 3, 2011

alfah alfah
Lab Rat
157 posts

cincirin,

I got rid of the errors but have the program running, but i cant find any tabs created.
Jus a blank screen !! :(

alfah

August 3, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Use this instead of last line in your ctor

  1. centralWidget->setLayout(mainLayout);

August 3, 2011

alfah alfah
Lab Rat
157 posts

denis,

i can jus use centralWidget?? wat is it??

i get the follwing error “ invalid use of member”.

alfah

August 3, 2011

Eddy Eddy
Area 51 Engineer
1296 posts

I think you need to use

  1. setCentralWidget
first

 Signature 

Qt Certified Specialist
Qt Ambassador

August 3, 2011

alfah alfah
Lab Rat
157 posts

:( it does work tht way either.

August 3, 2011

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

oops, I meant

  1. centralWidget()->setLayout(mainLayout);

Eddy, looks like it is form created from designer, so central widget should be there.

August 3, 2011

cincirin cincirin
Ant Farmer
387 posts

Tabs is the class derived from QMainWindow ? Can you show us the header and part of the code where you instantiate the Tabs class ?

August 3, 2011

alfah alfah
Lab Rat
157 posts

:D :D yeaa denis i got it right, Thank you!

so if i want to create buttons in the history tab? how to i go abt it?. I meant how do i acces each tab separately

alfah

August 3, 2011

Eddy Eddy
Area 51 Engineer
1296 posts
Did you use it like this ? :

this is the way moc does it when looking in the ui_tabs.h file

  1. centralwidget = new QWidget(Tabs); // it is just a temporary name
  2.                   ...
  3. Tabs->setCentralWidget(centralwidget);

@Denis
Yes, you are right!

EDIT : too late ;)

 Signature 

Qt Certified Specialist
Qt Ambassador

August 3, 2011

alfah alfah
Lab Rat
157 posts

eddy,

  1. Tabs::Tabs(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::Tabs)
  4. {
  5.     ui->setupUi(this);
  6.     tabWidget = new QTabWidget;
  7.     tabWidget->addTab(new QWidget,tr("History"));
  8.     tabWidget->addTab(new QWidget,tr("Calender"));
  9.     tabWidget->addTab(new QWidget,tr("Statistics"));
  10.     mainLayout = new QVBoxLayout;
  11.     mainLayout->addWidget(tabWidget);
  12.     centralWidget()->setLayout(mainLayout);
  13.    
  14.  
  15. }

i still do not know how to make individual buttons on each tab :(

alfa

August 3, 2011

Eddy Eddy
Area 51 Engineer
1296 posts

have a look at

  1. indexOf()

 Signature 

Qt Certified Specialist
Qt Ambassador

August 3, 2011

cincirin cincirin
Ant Farmer
387 posts

  1. QTabWidget::widget(int index)
return the tab page.
Then you can create buttons with returned parent.

August 3, 2011

alfah alfah
Lab Rat
157 posts

u mean tabWidget->indexOf()???

But i have not named each tab:(

August 3, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

alfah wrote:
eddy,

  1. Tabs::Tabs(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::Tabs)
  4. {
  5.     ui->setupUi(this);
  6.     tabWidget = new QTabWidget;
  7.     tabWidget->addTab(new QWidget,tr("History"));
  8.     tabWidget->addTab(new QWidget,tr("Calender"));
  9.     tabWidget->addTab(new QWidget,tr("Statistics"));
  10.     mainLayout = new QVBoxLayout;
  11.     mainLayout->addWidget(tabWidget);
  12.     centralWidget()->setLayout(mainLayout);
  13.    
  14.  
  15. }

i still do not know how to make individual buttons on each tab :(

alfa

You can event add custom widgets to the tab widget, so go this way:

  1. Tabs::Tabs(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::Tabs)
  4. {
  5.     ui->setupUi(this);
  6.     tabWidget = new QTabWidget;
  7.  
  8.     QWidget* pHistory = new QWidget;
  9.     // fill pHistory here
  10.     QPushButton* p = new QPushButton(tr("my text"), pHistory);
  11.  
  12.  
  13.     tabWidget->addTab(pHistory,tr("History"));
  14.     tabWidget->addTab(new QWidget,tr("Calender"));
  15.     tabWidget->addTab(new QWidget,tr("Statistics"));
  16.     mainLayout = new QVBoxLayout;
  17.     mainLayout->addWidget(tabWidget);
  18.     centralWidget()->setLayout(mainLayout);
  19.    
  20.  
  21. }

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

Page  
1

  ‹‹ Qt SDK for Meego 1.2 - Harmattan only ?      QT is very slow on windows CE if using raster painting-engine ››

You must log in to post a reply. Not a member yet? Register here!