Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Add a Widget to QScrollArea

Add a Widget to QScrollArea

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Allerknappe
    wrote on last edited by
    #1

    Hello,

    i want to add dynamicaly widgets to a QScrollArea.
    With my code i see the Labels, but can not scroll if a widget is outside of the visible scroll window.
    I thought first, its because the geometry settings are negativ, but also positive settings won´t work.
    Also i want to add the Widgets to an manual Layout.

    Maybe some help? Here my code(only the code for the scroll area) :

    QScrollArea *scrollArea;
    QWidget *scrollAreaWidgetContents;
    
    
    
    scrollArea = new QScrollArea(gridLayoutWidget);
    scrollArea->setObjectName(QStringLiteral("scrollArea"));
    scrollArea->setWidgetResizable(true);
    
    scrollAreaWidgetContents = new QWidget();
    scrollAreaWidgetContents->setObjectName(QStringLiteral("scrollAreaWidgetContents"));
    scrollAreaWidgetContents->setGeometry(QRect(0, 0, 347, 85));
    
    scrollArea->setWidget(scrollAreaWidgetContents);
    
    
    QScroller::grabGesture(scrollArea->viewport(), QScroller::LeftMouseButtonGesture);
    	
    QVariant OvershootPolicy = QVariant::fromValue<QScrollerProperties::OvershootPolicy>(QScrollerProperties::OvershootAlwaysOff);
    QScrollerProperties ScrollerProperties = QScroller::scroller(scrollArea->viewport())->scrollerProperties();
    ScrollerProperties.setScrollMetric(QScrollerProperties::VerticalOvershootPolicy, OvershootPolicy);
    ScrollerProperties.setScrollMetric(QScrollerProperties::HorizontalOvershootPolicy, OvershootPolicy);
    QScroller::scroller(scrollArea->viewport())->setScrollerProperties(ScrollerProperties);
    
    
    
    for(qint16 i = 0, ic = 0; i < ia ; i++)	//this loop builds the new message area
    	{
    //      label_2->setGeometry(QRect(30, 30, 131, 41));	geometry of the received messages
    //		label_3->setGeometry(QRect(80, 80, 131, 41));	geometry of the own messages
            if(i => label_2.size())
    		{
    			
                label_2.append(new QLabel(scrollAreaWidgetContents));
                
    			if(nachricht[ic].size() > 15)
    			{
    				sizeright = nachricht[ic].size() -15;
                    label_2[i]->setText(nachricht[ic].left(15)+ "/n"+nachricht[ic].right(sizeright));
    			}
    			else
                    label_2[i]->setText(nachricht[ic]);
    				
    			ic++;
    		}
    
    		label_2[i]->show();		// with show the label widget will be visible
    		
    		label_2[i]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            label_2[i]->setGeometry(QRect(recx, 80-50*pos, 131, 41));
    		
    		
    		pos--;
    	}
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Since you are not using any layout you have to also resize scrollAreaWidgetContents by hand. I'd recommend changing your current technique by using a QVBoxLayout that you set on scrollAreaWidgetContents and then append your labels to it.

      Depending on what you want to do, maybe a QListView with a custom model might be simpler. You may need a QStyledItemDelegate depending on what you want to show and how you want to show it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Allerknappe
        wrote on last edited by
        #3

        ok SGaust thanks for the post.

        i tried to use not an layout, because i want to change the width of the label and it´s maybe smoother without layout...

        so i tried to resize the widget with:

         scrollAreaWidgetContents->resize(70,30*pos);
        scrollAreaWidgetContents->update();
           scrollArea->setViewport(scrollAreaWidgetContents);
        

        but i get still the same result -.-

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Allerknappe said in Add a Widget to QScrollArea:

          i want to change the width of the label

          Hi
          Just as note. With layouts you can always change minimumWidth and get that width
          so i would not say its smoother without layouts considering what else they offer :)

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved