QScrollArea not showing scrollbars and messing widgets



  • Hi folks,

    I have a widget with a margin of 8px. Inside it, I have a QVBoxLayout having only a QScrollArea. (Sounds stupid, but I did it because I can't find a way to separate the QScrollArea of the widget borders. Otherwise, I'd have only a QScrollArea with the VBoxLayout inside.)

    Now the real problem: the QScrollArea contains another QVBoxLayout. But when I resize the window, it's contents cannot be seen and I don't get any scrollbar.

    The normal widget is this (does anyone here like Cardigans?):

    !http://img28.imageshack.us/img28/9400/widgetnormal.png(Widget normal)!

    And the resized widget is this:

    !http://img130.imageshack.us/img130/8243/widgetresized.png(Widget resized)!

    The current code:

    @
    setContentsMargins(QMargins(8, 8, 8, 8));
    QScrollArea *scrollArea = new QScrollArea(this);
    scrollArea->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
    scrollArea->setMidLineWidth(2);
    scrollArea->setLineWidth(2);

    // Set background and foreground colors
    QPalette contextPalette = scrollArea->palette();
    contextPalette.setBrush(backgroundRole(), QColor(255,255, 255));
    contextPalette.setColor(foregroundRole(), QColor(100,100,100));
    scrollArea->setPalette(contextPalette);
    scrollArea->setAutoFillBackground(true);
    
    artistLabel = new QLabel(this);
    artistLabel->setWordWrap(true);
    QFont artistFont = artistLabel->font();
    artistFont.setBold(true);
    artistFont.setPointSize(artistFont.pointSize() + 4);
    artistLabel->setFont(artistFont);
    artistLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Maximum);
    
    pictureLabel = new QLabel(this);
    
    summaryLabel = new QLabel(this);
    summaryLabel->setOpenExternalLinks(true);
    summaryLabel->setWordWrap(true);
    QFont summaryFont = summaryLabel->font();
    summaryFont.setPointSize(summaryFont.pointSize() + 2);
    summaryLabel->setFont(summaryFont);
    summaryLabel->setTextInteractionFlags(Qt::TextBrowserInteraction | Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse);
    summaryLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Maximum);
    
    moreLinkLabel = new QLabel(this);
    QFont moreLinkFont = moreLinkLabel->font();
    moreLinkFont.setItalic(true);
    moreLinkLabel->setFont(moreLinkFont);
    moreLinkLabel->setOpenExternalLinks(true);
    
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(QMargins(15, 15, 15, 15));
    layout->addWidget(artistLabel, 0, Qt::AlignTop);
    layout->addSpacing(20);
    layout->addWidget(pictureLabel, 0, Qt::AlignTop | Qt::AlignHCenter);
    layout->addSpacing(20);
    layout->addWidget(summaryLabel, 0, Qt::AlignTop);
    layout->addSpacing(20);
    layout->addWidget(moreLinkLabel, 0, Qt::AlignTop | Qt::AlignRight);
    layout->addStretch(0);
    scrollArea->setLayout(layout);
    resetLabels();
    
    QFrame *frame = new QFrame(this);
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(scrollArea);
    setLayout(mainLayout);
    

    @



  • I solved my problem making the widget inherit QScrollArea and replacing the code above for this:

    @
    ContextWidget::ContextWidget(QWidget *parent) :
    QScrollArea(parent)
    {
    setStyleSheet("margin: 16px;"); // Makes scrollbar invisible!
    setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
    setMidLineWidth(2);
    setLineWidth(2);

    // Set background and foreground colors
    QWidget *widget = new QWidget(this);
    QPalette contextPalette = widget->palette();
    contextPalette.setColor(backgroundRole(), QColor(255,255, 255));
    contextPalette.setColor(foregroundRole(), QColor(100,100,100));
    widget->setPalette(contextPalette);
    widget->setAutoFillBackground(true);
    
    artistLabel = new QLabel(this);
    artistLabel->setWordWrap(true);
    QFont artistFont = artistLabel->font();
    artistFont.setBold(true);
    artistFont.setPointSize(artistFont.pointSize() + 4);
    artistLabel->setFont(artistFont);
    artistLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Maximum);
    
    pictureLabel = new QLabel(this);
    
    summaryLabel = new QLabel(this);
    summaryLabel->setOpenExternalLinks(true);
    summaryLabel->setWordWrap(true);
    QFont summaryFont = summaryLabel->font();
    summaryFont.setPointSize(summaryFont.pointSize() + 2);
    summaryLabel->setFont(summaryFont);
    summaryLabel->setTextInteractionFlags(Qt::TextBrowserInteraction | Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse);
    summaryLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Maximum);
    
    moreLinkLabel = new QLabel(this);
    QFont moreLinkFont = moreLinkLabel->font();
    moreLinkFont.setItalic(true);
    moreLinkLabel->setFont(moreLinkFont);
    moreLinkLabel->setOpenExternalLinks(true);
    
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(QMargins(15, 15, 15, 15));
    layout->addWidget(artistLabel, 0, Qt::AlignTop);
    layout->addSpacing(20);
    layout->addWidget(pictureLabel, 0, Qt::AlignTop | Qt::AlignHCenter);
    layout->addSpacing(20);
    layout->addWidget(summaryLabel, 0, Qt::AlignTop);
    layout->addSpacing(20);
    layout->addWidget(moreLinkLabel, 0, Qt::AlignTop | Qt::AlignRight);
    layout->addStretch(0);
    widget->setLayout(layout);
    setWidget(widget);
    setWidgetResizable(true);
    resetLabels();
    

    }
    @

    But the line

    @
    setStyleSheet("margin: 16px;");
    @

    is making the scrollbars disappear! Is this some Qt bug or am I missing something?



  • try this instead:
    @
    setStyleSheet("QAbstractScrollArea { margin: 16px; }");
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.