PDA

View Full Version : tabWidget duplicate frames



ilmandorlone
15th March 2016, 11:32
hello I am marco and I am new in the forum

I just started with QT but I'm a autodidact programmer c ++ for a long time

I wanted to create a tab with a distinctive form and each time you create a new tab window I have always the same layout and my basic forms

11790



void MainWindow::on_tabWidget_currentChanged(int index)
{
qDebug() << n_tab;
if (index == n_tab){
ui->tabWidget->setTabText(n_tab, "QString::number(n_tab)");

QWidget *tabWidget;
tabWidget = new QWidget;

// ===============================================
// copi form from tab 1 end paste on new tabWidget
// ===============================================

ui->tabWidget->addTab(tabWidget,"+");
n_tab ++;
}
}


in this case if we press the "+" tab add a new tab
I would like already I said the nuvo widget is equal to the first


Thank you in advance

anda_skoa
15th March 2016, 11:57
Design the page widget as a separate form (New -> Qt -> Qt Designer Form Class) and then create instances of that widget instead of QWidget.

Cheers,
_

ilmandorlone
15th March 2016, 15:10
thank you so much
as associate now my widget class to the new tab?
tabwidget is my class



void MainWindow::on_tabWidget_currentChanged(int index)
{
qDebug() << n_tab;
if (index == n_tab){
ui->tabWidget->setTabText(n_tab, "new");

tabSerial *tabWidget;
tabWidget = new tabSerial;

ui->tabWidget->addTab(tabWidget,"+");
n_tab ++;
}
}

anda_skoa
15th March 2016, 15:36
I am not sure what you are asking, this looks ok.

Three things I would have done differently:
- ui->tabwidget->count() instead of n_tab, no point in risking a variable getting out of syn
- TabSerial instead of tabSerial, classes usually start with an upper case character in Qt apps
- avoid using of "connect by name" and use explicit connect() instead.

Cheers,
_

ilmandorlone
15th March 2016, 17:15
ok sorry

I modified the class as you told me but I did not understand how to use the connect ()



void MainWindow::on_tabWidget_currentChanged(int index)
{
qDebug() << index;
if (index == ui->tabWidget->count()-1){
ui->tabWidget->setTabText(ui->tabWidget->count()-1, "port");
tabWidget *serialtab;
serialtab = new tabWidget();

ui->tabWidget->addTab(serialtab,"+");
}
}



now my class isthis
11791

please can help me with an example

anda_skoa
15th March 2016, 18:03
The explicit connect would look something like this


MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);

connect(ui->tabWidget, &QTabWidget::currentChanged, this, &MainWindow::onCurrentTabChanged);
}

where onCurrentTabChanged would be the function you currently have, just with a different name. Can be any valid method name in that class.

Cheers,
_

ilmandorlone
15th March 2016, 21:09
really thank you for your patience.

I'm sorry but I have not figured out how to attach my widget to the new tab. :(

in my example the new tab windows are always empty and you do not see the widget

mynamebilalzahid
16th March 2016, 07:10
where will you run the tab ?

anda_skoa
16th March 2016, 08:38
I'm sorry but I have not figured out how to attach my widget to the new tab. :(

in my example the new tab windows are always empty and you do not see the widget

addTab() should do that.
Does your widget work stand alone?
E.g. testing as a separate window by calling serialTab->show() instead of passing it to addTab?

Cheers,
_

ilmandorlone
16th March 2016, 10:54
you can see my screensaver of my UI class
11794

and this is the cpp file of the class



#include "tabwidget.h"
#include "ui_tabwidget.h"

tabWidget::tabWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::tabWidget)
{
ui->setupUi(this);
}

tabWidget::~tabWidget()
{
delete ui;
}


then in mainwindow.cpp I include the class with

#include "tabwidget.h"

in the end always How do I create a new tab using my widget class, but the whole thing does not work


tabWidget *serialtab;
serialtab = new tabWidget();

ui->tabWidget->addTab(serialtab,"+");



I hope I misunderstood your question, thanks

Added after 21 minutes:

very sorry.
the problem was that the first 2 tabs are already created by mainwindow.ui but did not have the calss TabWidget created by me but the third tab it works !!!

thank you very much !!! :) :) :)