PDA

View Full Version : QScrollArea?



kw
17th March 2006, 02:15
Hi,

I have a custom widget which I want to place into a QScrollArea, which in turn goes on to a QMainWindow. This is done in a few examples, and I think my code pretty much matches theirs, but I don't seem to get the scroll bars when I resize my window. I am using a QLabel with an image in it for testing. The code in my mainwindow constructor is:


label = new QLabel;
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath());

QImage image(fileName);
label->setPixmap(QPixmap::fromImage(image));

scroll = new QScrollArea;
scroll->setWidget(label);

setCentralWidget(scroll);


Any advice or ideas what Im doing wrong?
The image is displayed on the label, but when I resize the main window to something smaller than the image, no scrollbars appear.. Where am I going wrong? Do I need additional code?

Thx in advance,
kw

Sarma
17th March 2006, 04:54
hi kw,
I don't see much wrongs in your code. But try this line instead of line 9 in your code:

scroll->addChild(label);
This would be of some help. But Iam not getting the complete idea of what you want to do.You are saying that you are putting an image in a label.How is that done?
Regards,
Sarma.

zlatko
17th March 2006, 08:04
try this ...


scroll->resizeContest(label.width(),label.height());

jnana
17th March 2006, 09:46
Just try with large label size.

label->resize(1000, 1000);

kw
17th March 2006, 14:24
Phew, found it at last :)

A few weird typos there btw guys I think.. QScrollArea doesn't have an addChild member function, and I think setWidget is the way to go.. Since it needs to be linked to a single widget. Maybe the (now deprecated) QScrollView was the one you were thinking of?
I assume that's also the class that had the resizeContents method (the typo in that made me laugh tho ;-)).. I tried resize() which is present, but that didn't do much :(
Resizing the label, like jnana said did have some effect. But it still didn't work/look right.. It showed me what I was doing wrong though.. I was using generated code for the dialog (Im developing in VS with VS Integration), and calling ui.setupUi(this); to create all the required UI parts and connections. The problem was that my code was *before* this GUI setup code, so some parts probably didn't exist yet.. :o
I put my code *after* the ui.setupUI(this) code, and now it works perfectly. :D

-kw

kw
17th March 2006, 14:40
I just checked the generated code, and it shows:


QWidget *centralWidget;
...
centralWidget = new QWidget(NforceClass);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
NforceClass->setCentralWidget(centralWidget);

Which is presumably what caused all the problems. Now that my own code comes after this, it simply overwrites the centralWidget setting.. But I'm wondering if there isn't some way to remove the centralWidget entirely in that, so that it doesn't get generated anymore? I can see it in my Object Inspector, but that doesn't allow me to do anything with it. I also can't find any scroll area in my Qt Toolbox, so setting a scrollarea as central widget in the form editor is also out of the question I suppose.. It just seems sorta sloppy to have dead code that gets overwritten in the source.
Any ideas?

-kw