PDA

View Full Version : QTabBar??



anupamgee
30th April 2009, 08:18
hi all,
I want to set image on a tab of a tabbar .I want to create tab as the same size of image.
But size of tab i m creating is bigger than that of image........
I used the following code


QTabBar *tabbar=new QTabWidget(this);
QPixmap image(":/images/abc.png");
QIcon icon(image);
tabbar->addTab(icon," ");
QSize size(image.width(),image.height());
tabbar->setIconSize(size);



How to do????

e8johan
30th April 2009, 08:48
You don't really describe your problem, but I suspect that you want to stretch your image. This can be done, however, I've run into issues with the QTabBar where I've wanted special looks and I've always ended up subclassing it and reimplementing the painting - just to get control of the size of each tab, etc.

anupamgee
30th April 2009, 09:04
thnx for reply,
I want to create tab of same size of my image..
how to do that? I m using the code mentioned in previous post.
but using that code i m not getting the tab size as that of image.
how to change the size of tab ?

e8johan
30th April 2009, 09:18
In what contents are you using your tabbar? As a part of a QTabWidget, or in another setting.

anupamgee
30th April 2009, 09:20
As a part of QTabWidget...

e8johan
30th April 2009, 09:30
I seem to recall that you could set the QTabBar widget for a QTabWidget (cannot seem to locate the method in the 4.5 docs). In that case, you can reimplement the virtual methods at the bottom of this details section: http://doc.trolltech.com/4.5/qtabbar.html#details . Especially: http://doc.trolltech.com/4.5/qtabbar.html#tabSizeHint .

e8johan
30th April 2009, 09:31
Here is the method that I mentioned: http://doc.trolltech.com/4.5/qtabwidget.html#setTabBar . It is protected, so you will have to inherit QTabWidget and call it from your new c'tor.

faldzip
30th April 2009, 12:04
first of all, this cant really do much more than segmentation fault:


QTabBar *tabbar=new QTabWidget(this);

you create pointer to QTabBar and make it pointing to QTabWidget...
tell me, what this code does:


QRect rect = tabbar->tabRect(0);

?

If you want to get the tabbar used in QTabWidget, you have to subclass QTabWidget, because tabBar() and setTabBar() are protected methods.

anupamgee
30th April 2009, 13:24
hi,
this is typing mistake here
I used


QTabBar *tabbar=new QTabBar(this);


I want to change the size of Tabs in TabBar ...What to do to change it.
thnx

e8johan
30th April 2009, 13:48
Inherit QTabBar, e.g.


class MyTabBar : public QTabBar { ... };

Re-implement the tabSizeHint function http://doc.trolltech.com/4.5/qtabbar.html#tabSizeHint .

Inherit QTabWidget:


class MyTabWidget : public QTabWidget { ... };

In your constructor, call setTabbar( ) and make it use MyTabBar.

anupamgee
30th April 2009, 15:01
i used tabSizeHint () in MyTabBar class inherited from QTabBar class.


class MyTabBar:public QTabBar
{
public:
MyTabBar(QWidget *parent):QTabBar(parent)
{
QIcon callIcon(":/images/a.png");
this->addTab(callIcon," ");
QSize size=this->tabSizeHint(1);
this->setIconSize(size);

}
};


but i didn't get wat u mean by reimplementing tabsizehint()??

e8johan
30th April 2009, 17:15
You should not put your code in the QTabBar c'tor. You should re-implement, that is override a virtual method, the size hint function.

You're then supposed to inherit the QTabWidget from which you use your QTabBar.

Then, your code uses your tab widget instead of QTabWidget.

faldzip
30th April 2009, 19:13
In other way you have to redefine the tabSizeHint(), because you will not use it, but other objects will, so you have to tell them what the sizeHint is. And you can do it in tabSizeHint(). Something like this:
Let's say that ClassA uses ClassB and want to know what is the object of ClassB size just to know how to paint it. So the code can be:



class ClassB
{
public:
. . .
QSize sizeHint() const { return QSize(100, 100); }
};

class ClassA
{
private:
ClassB classb;
public:
void paint() {
QRect rect(QPoint(0, 0), classb.sizeHint());
paintSomethingInRect(something, rect);
}
};

In your situation ClassB = QTabBar and ClassA = QTabWidget; And as you see QTabWidget and some QTabBar methods want to know how big tabbar is so they ask it through tabSizeHint() method. That's why you have redefined the tabSizeHint().

P.S. By the way: these things, like "subclassing", "override" and so on are basics of C++ programming, so I think you should equipped yourself with some C++ books/tutorials/websites.