PDA

View Full Version : ScrollArea in a tabWidget



Yaoming
18th January 2014, 10:21
Hello there :)
Here's my problem: I've got this application: 9945
My left widget is a TabWidget in which I can add some textEdits. Those textEdit adapt their size to the content they have (they grow bigger each time you write something in them). My problem is the tab widget I got is far too small and I would like to be able to scroll it when my textEdit becomes to big to be fully displayed in it.

I've tried to create a QScrollArea but the scrollbars are not showing up:

//here's the constructor
scrollArea = new QScrollArea(this); //i've created a QScrollArea* in my .h
scrollArea->setWidgetResizable(true);
ui.Onglets->insertTab(0, scrollArea, "Kayaaa");//i try to add my scrollArea to my tabWidget but it creates a new tab and I would like the scrollArea to be in my existing tabs

anda_skoa
18th January 2014, 13:16
I am not sure I understand the problem, but why not add the scrolll area in designer?

Cheers,
_

Yaoming
18th January 2014, 17:33
I've tried to do it on Designer but it doesn't work. My problem is: I add some textEdits which have my qtabWidget as "parent". Those textEdit grow bigger each time you add text in them (so that you always see the full text). But when they are bigger than the qtabwidget (they need to be because the user needs to have plenty of space to be able to write a lot of things) I would like my QTabWidget to display a vertical scrollbar so that the user can see what he has been writing.
My problem with designer is that the scrollArea is that it doesn't take my qtabWidget size (it's not taking the entire tabs and it doesn't display the scrollbars).
My problem with my code is that I've got to create a new Tab to make the scrollArea take the entire size of my tab and it's not working. On top of that I'd like my background to be white and not grey as it is when I compile. Here's a screenshot of what I have:

with designer: 9946
with my code: 9947

anda_skoa
18th January 2014, 18:52
Sounds like your tab is missing the layout. Check that you have a layout assigned to the tab.

Cheers,
_

Yaoming
18th January 2014, 20:44
There's one (I think). Here's a screenshot of my QtDesigner for my QTabWidget (named "Onglets")9948

anda_skoa
19th January 2014, 10:42
Indeed. Since it is a QGridLayout the problem could be that the scroll area is not spanning across all columns. This can be a bit tricky in designer but is doable with a bit of experiementation.

However, now that the "main" widget of the tab should be the scroll area, maybe you can reduce the number of spacers, e.g. just one horizontal and one vertical to keep the button in the top right corner.
This should make it easier to place the scroll area.

Cheers,
_

Yaoming
19th January 2014, 12:04
I've done what you told me but it's still not working. Here's what I have: 99499950.
I don't know how to have just one scrollbar that would be shown whenever my current tab content is bigger than the tab's size. I could use a QVerticalScrollBar perhaps? That might be harder I don't know...

anda_skoa
19th January 2014, 16:30
This looks like the text edit being a cild of the tab instead of being inside the scrollarea.

Since you want more than one widget inside the scrollarea, add a normal and empty QWidget into it, see QScrollArea::setWidget()
Then add your text edits to that widget, using whatever layout you find appropriate.

Cheers,
_

Yaoming
19th January 2014, 16:47
I've done what you told me but the scrollbars are not showing up. My code:


//my constructor
container = new QWidget(); //the empty widget
ui.scrollArea->setWidget(container); //that I add to my scrollArea
//this my text Edit's creation
txt = new QTextEdit(container); //I create my textEdit and set its parent to the created widget
connect(txt->document(), SIGNAL(contentsChanged()), this, SLOT(update_size()));
txt->installEventFilter(this);
txt->setFont(QFont("Times New Roman", fontSize));
txt->setTextColor(fontColor);
txt->show();

anda_skoa
19th January 2014, 18:40
The container does not have a layout. So if the size of the text edit increases it does not increase the size of the container.

Cheers,
_

Yaoming
19th January 2014, 20:47
How do you suggest that I do that? I tried that but still not working:
container = new QWidget();
layout = new QVBoxLayout();
ui.scrollArea->setWidget(container);
container->setLayout(layout);
ui.scrollArea->setWidgetResizable(true);

anda_skoa
19th January 2014, 21:25
Yes, like that.

I just did a quick experiement in designer and I get scroll bars when the scroll area is smaller than the text edits inside it.
9952

Cheers,
_

Yaoming
20th January 2014, 12:17
Could you please give me your code? Because what I have is not working... I've tried different ways but in vain.

anda_skoa
20th January 2014, 13:46
The designer ui file is attached, the Form.ui is a link.

Cheers,
_

Yaoming
20th January 2014, 17:25
Ok but you did it with Designer. In my case, I can't... Here's all my code for this part, do you see any mistakes? Would you have done it differently?


//my .h file
QVBoxLayout* layout;
QTextEdit* txt;
QWidget* container;

//my .cpp file
//Constructor
container = new QWidget();
layout = new QVBoxLayout();
ui.scrollArea->setWidget(container);
container->setLayout(layout);
ui.scrollArea->setLayout(layout);
ui.scrollArea->setWidgetResizable(true);

//Slot which creates my textEdits
txt = new QTextEdit();
txt->setParent(container);
connect(txt->document(), SIGNAL(contentsChanged()), this, SLOT(update_size()));
txt->installEventFilter(this);
txt->setFont(QFont("Times New Roman", fontSize));
txt->setTextColor(fontColor);
txt->show();

anda_skoa
20th January 2014, 19:02
You are missing the layout->addWidget(txt) call.

Cheers,
_

Yaoming
20th January 2014, 19:51
Still not working... Anyway you've helped me a lot so I'm going to try and figure it out by myself and stop bothering you.
Thanks for your help :)