PDA

View Full Version : Can Qt open a bottle?



sivrisinek
17th July 2008, 16:05
Hi there,

i have the frequently annoying question (problem) which probably many have faced before:

" hide a tab page "

1) without using removeTab() (because it will be deleted)
2) without using setTabEnabled() (because with CTRL+TAB one can indeed open it!!!!)
3) without using QWidgetStack (because it is a part of Qt3)

is there a solution for this problem? Please notice the nice BUG in point (2). If you would like to care for data integration, this might be a problem or not?

Qt does all those fascinating things like driving a car but can not open a bottle (or close it if you would prefer)?

:eek:

please excuse me for this way of articulation of the problem.

wysota
17th July 2008, 16:35
1) without using removeTab() (because it will be deleted)

It will not be deleted. You can store a pointer to it and reinsert the tab later on.


3) without using QWidgetStack (because it is a part of Qt3)
See QStackedWidget.

luf
17th July 2008, 17:27
yes, qt can open a bottle if you make a serial port device with a robot arm, program a nice gui with image recognition through a webcam, that finds the bottle and opens it...

shouldn't take more than a few hours I guess :D

For a TabWidget that can add/remove pages, but still remember them, use a stackedwidget where you keep the widgets always, and use a TabBar with indexing, where you update the index when you add/remove tabs.

I have not tested the below code, as it's a rewrite of something I did a few months ago, but you will get a hint on where to start off.

header:


class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent);
};

class QMyCustomTabBar : public QTabBar
{
Q_OBJECT
public:
QMyCustomTabBar(QWidget *parent);
QList <unsigned short int> TabPage;

public slots:
PageToView(int index);
RemoveTabWithNo(unsigned short int stackedpagenumber);
InsertTabWithNo(unsigned short int stackedpagenumber, const QString &TabString);

signals:
SendPageToView(unsigned int index);
};

source:


MyWidget::MyWidget(QWidget *parent) : QWidget (parent)
{
QStackedWidget *myStacked = new QStackedWidget(this);
myStacked->addWidget(widget1);
myStacked->addWidget(widget2);
myStacked->addWidget(widget3);

QMyCustomTabBar *myCustomTabBar = new QMyCustomTabBar(this);
connect (myCustomTabBar, SIGNAL(SendPageToView(int)), myStacked, SLOT(setCurrentIndex(int)));
// then use RemoveTabWithNo and InsertTabWithNo to remove/insert.
// subclass checkbox'es or add slots for checkbox'es if you need those to enable disable
}

QMyCustomTabBar::QMyCustomTabBar(QWidget *parent) : QTabBar (parent)
{
connect (this, SIGNAL(currentChanged(int)), this, SLOT(PageToView(int)));
}

void QMyCustomTabBar::PageToView(int index)
{
if (index < this->TabPage.count())
{
emit(SendPageToView(TabPage[index]));
}
}
void QMyCustomTabBar::RemoveTabWithNo(unsigned short int stackedpagenumber)
{
for (int i = 0;i < this->TabPage.count(); i++)
{
if (TabPage[i] == stackedpagenumber)
{
TabPage.removeAt(i);
this->removeTab(i);
return;
}
}
}

void QMyCustomTabBar::InsertTabWithNo(unsigned short int stackedpagenumber, const QString &TabString)
{
// see if we have a tab we need to insert it before...
for (int i = 0;i < this->TabPage.count(); i++)
{
if (TabPage[i] > stackedpagenumber)
{
TabPage.insert(i, stackedpagenumber);
this->insertTab(i, TabString);
return;
}
}
// if not, we should insert at the end
this->TabPage.append(stackedpagenumber);
this->addTab(TabString);
}

sivrisinek
18th July 2008, 09:16
Thanks a lot for the answers.

Have fun.

sivrisinek
18th July 2008, 10:39
It will not be deleted. You can store a pointer to it and reinsert the tab later on.


See QStackedWidget.

What can I do to get the pointer to a specified tabpage? There is not a member method of QTabWidget which returns a QWidget* to the removed page. Or at least I can not find it.

Here the question is not to hide the whole tab widget; just a single page of it.
---

My Qt Version is 4.2.2, that's why QStackedWidget was documented to belong to Qt3 I guess.

sivrisinek
18th July 2008, 10:45
it is very important for us to hide the page (and not re-instantiate it) as it has much more complex widgets inside and not few buttons and labels..

Raccoon29
18th July 2008, 10:51
There is not a member method of QTabWidget which returns a QWidget* to the removed page.

If you know the index of the page to be removed, you might use QTabWidget::widget(int index) to retrieve the tab's pointer before removing it.
Then you can easily restore it with QTabWidget::addTab(QWidget*,QString) or QTabWidget::insertTab(int,QWidget*,QString).
I never tested it, but it should work. Shouldn't it?

EDIT: at the moment is impossible to hide litterally the tab, so this trick seems required. Anyway since you save a pointer to the page, probably its content shall be saved to the restoring too.

sivrisinek
18th July 2008, 11:35
If you know the index of the page to be removed, you might use QTabWidget::widget(int index) to retrieve the tab's pointer before removing it.
Then you can easily restore it with QTabWidget::addTab(QWidget*,QString) or QTabWidget::insertTab(int,QWidget*,QString).
I never tested it, but it should work. Shouldn't it?

EDIT: at the moment is impossible to hide litterally the tab, so this trick seems required. Anyway since you save a pointer to the page, probably its content shall be saved to the restoring too.

Thanks a lot this did really work.

jpujolf
28th April 2009, 16:25
If anyone want an "embeded" solution to this problem, can freely use QExtendedTabWidget (http://www.qt-apps.org/content/show.php/QExtendedWidgets?content=103163)

Have fun.