[Qt-interest] how to add a scrollBar in QTabWidget

Stefan Löffler st.loeffler at gmail.com
Thu Jul 14 15:40:14 CEST 2011


Hi,

On 2011-07-14 15:02, Sujan Dasmahapatra wrote:
> Hi Samuel
> In the last example if I add a layout in widget and then put all the
> buttons in the layout then those buttons are being resized to fit the
> screen. I dont want that, They should appear as it is  and a scroll
> bar should come. I am not getting any scrollbar in the tabwidget.
>  
> Can u tell me  anyone whats wrong in this code below..

It doesn't compile as is, there are things missing (e.g., a main()
function), and there are typos in it (e.g., "TabWdiget"). Seriously, if
you want to provide a minimal example, make sure it works!

Regarding the invisibility of the buttons and scroll bar: it seems you
need to explicitly set the size of the scroll bar area, or else it
defaults to size zero - which is too small to fit the buttons in. Here's
a minimal example that's working for me:

#include <QtGui>

///////////////////////////////
class TabWidget : public QTabWidget
{
public:
    TabWidget() {
        for(int i=0; i<10; i++) {
            name.append("Page" + QString::number(i+1));
            areas.append(new QScrollArea(this));
            pages.append(new QWidget(areas.last()));

            areas.last()->setWidget(pages.last());
            addTab(areas.last(),name.last());

            int coordx = 20;
            int coordy = 20;
            int w = 50;
            int h = 25;
            int incre = 0;

            pages.last()->setGeometry(0, 0, 200, 200);

            for(int j=0; j<20; j++) {
                buttons.append(new QPushButton("button " +
QString::number(j+1), pages.last()));
                buttons.last()->setGeometry(coordx,coordy+incre,w,h);
                buttons.last()->show();
                incre += h+10;
            }
        }
    }
    ~TabWidget() { }
public:
    QStringList name;
    QList<QScrollArea *> areas;
    QList<QWidget *> pages;
    QList<QPushButton *> buttons;
};


int main(int argc, char * argv[])
{
    QApplication app(argc, argv);

    TabWidget w;
    w.show();

    app.exec();
}




HTH
Stefan



More information about the Qt-interest-old mailing list