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. How to add scroll bar action

How to add scroll bar action

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.8.0qt 5.7scroll barscrolling
12 Posts 3 Posters 6.1k Views 2 Watching
  • 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.
  • K Kinesis

    I want to add scroll bar that can be scrolled down and up until the top or bottom of my list widget . I already add the scroll bar but not the action.How can I add action of my scroll bar.I would appreciate any answers.
    Here is my code: mainwindow.cpp

        ui->listWidget_2->setFlow(QListView::LeftToRight);
        ui->listWidget_2->setMinimumSize(1000,740);
        ui->listWidget_2->setGridSize(QSize(1200, 410));
        ui->listWidget_2->setResizeMode(QListView::Adjust);
        ui->listWidget_2->setViewMode(QListView::ListMode);
        ui->listWidget_2->setWrapping(true);
        
        
        
        
        
        ui->listWidget_image->setFlow(QListView::LeftToRight);
        // ui->listWidget_image->setMinimumSize(1000,340);
        ui->listWidget_image->setGridSize(QSize(380, 360));
        ui->listWidget_image->setResizeMode(QListView::Adjust);
        ui->listWidget_image->setViewMode(QListView::ListMode);
        //ui->listWidget_image->setViewMode(QListWidget::IconMode);
        //ui->listWidget_image->setIconSize(QSize(400,360));
        // ui->listWidget_image->setResizeMode(QListWidget::Adjust);
        ui->listWidget_image->setWrapping(true);
        
        
        
        ui->listWidget_read->setFlow(QListView::LeftToRight);
        //ui->listWidget_read->setMinimumSize(860,380);
        ui->listWidget_read->setGridSize(QSize(380, 360));
        ui->listWidget_read->setResizeMode(QListView::Adjust);
        ui->listWidget_read->setViewMode(QListView::ListMode);
        ui->listWidget_read->setWrapping(true);
        auto    item = new QListWidgetItem("", ui->listWidget_2);
        auto    widget = new QWidget;
        auto    vb = new QVBoxLayout;
        auto    hb = new QHBoxLayout;
        vb->addWidget(ui->pushButton);
        
        
        vb->addWidget(ui->listWidget_image);
        vb->addWidget(ui->listWidget_read);
        hb->addLayout(vb);
        hb->addWidget(ui->verticalScrollBar);
        widget->setLayout(hb);
        widget->setMinimumSize(1200,730);
        ui->listWidget_2->setItemWidget(item,widget);
        
        
        QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",
                                                           
                                                           QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
        
        
        
        
        directory.setNameFilters({"*.log", "*.txt" , "*.sh" , "*.yml"});
        
        for(const QFileInfo & finfo: directory.entryInfoList()){
            
            m_textEdit = new QTextEdit;
            m_textEdit -> setReadOnly(true);
            QFile data(finfo.absoluteFilePath()) ;
            data.open(QIODevice::ReadOnly);
            QTextStream stream(&data);
            QString content = stream.readAll();
            data.close();
            
            m_textEdit->setText(content);
            auto    item = new QListWidgetItem("", ui->listWidget_read);
            auto    widget = new QWidget;
            auto    label = new QLabel(finfo.fileName());
            auto    vb = new QVBoxLayout;
            
            vb->addWidget(label);
            vb->addWidget(m_textEdit);
            
            widget->setLayout(vb);
            widget->setMinimumSize(340, 340);
            ui->listWidget_read->setItemWidget(item, widget);
            
            
            
        }
        
        
        
        
        directory.setNameFilters({"*.mp4" , "*.avi" , "*.flv" , "*.mwv"});
        
        for(const QFileInfo & finfo: directory.entryInfoList()){
            QMediaPlayer *mediaPlayer = new QMediaPlayer();
            mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
            videoItem = new QGraphicsVideoItem;
            videoItem->setSize(QSize(320,240));
            QGraphicsScene *scene = new QGraphicsScene(this);
            QGraphicsView *graphicsView = new QGraphicsView(scene);
            mediaPlayer->setVideoOutput(videoItem);
            
            QPushButton *m_playButton = new QPushButton();
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
            
            connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() {
                switch (mediaPlayer->state()) {
                case QMediaPlayer::PlayingState:
                    mediaPlayer->pause();
                    break;
                default:
                    mediaPlayer->play();
                    break;
                }
            });
            
            
            connect(mediaPlayer, &QMediaPlayer::stateChanged, [m_playButton, this](QMediaPlayer::State state) {
                switch(state) {
                case QMediaPlayer::PlayingState:
                    m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
                    break;
                default:
                    m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
                    break;
                }
            });
            
            
            
            QSlider *m_positionSlider = new QSlider(Qt::Horizontal,this);
            m_positionSlider->setRange(0,mediaPlayer->duration() / 1000);
            
            
            connect(mediaPlayer, &QMediaPlayer::positionChanged ,[m_positionSlider, this](qint64 position){
                
                m_positionSlider->setValue(position);
            });
            
            connect(mediaPlayer, &QMediaPlayer::durationChanged ,[m_positionSlider, this](qint64 duration){
                
                m_positionSlider->setRange(0,duration);
            });
            
            
            connect(m_positionSlider ,&QAbstractSlider::sliderMoved, [mediaPlayer ,this] (int position){
                
                mediaPlayer->setPosition(position);
            });
            
            
            
            
            auto    item = new QListWidgetItem("", ui->listWidget_image);
            auto    widget = new QWidget;
            auto    label = new QLabel(finfo.fileName());
            auto    vb = new QVBoxLayout;
            
            QBoxLayout *controlLayout = new QHBoxLayout;
            controlLayout->setMargin(0);
            controlLayout->addWidget(m_playButton);
            controlLayout->addWidget(m_positionSlider);
            
            vb->addWidget(label);
            vb->addWidget(graphicsView);
            vb->addLayout(controlLayout);
            widget->setLayout(vb);
            widget->setMinimumSize(340, 340);
            ui->listWidget_image->setItemWidget(item,widget);
            player.append(mediaPlayer);
            scene->addItem(videoItem);
            
            
            
        }
    
    jsulmJ Online
    jsulmJ Online
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Kinesis What action do you mean?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    K 1 Reply Last reply
    0
    • jsulmJ jsulm

      @Kinesis What action do you mean?

      K Offline
      K Offline
      Kinesis
      wrote on last edited by
      #3

      @jsulm
      I just need to scroll down or up to see the items . I can't see all of my items on the display because of the listWidget frame size .That's why I need to scroll down to see other items . I don't know how can I add scrolling action .

      jsulmJ 1 Reply Last reply
      0
      • K Kinesis

        @jsulm
        I just need to scroll down or up to see the items . I can't see all of my items on the display because of the listWidget frame size .That's why I need to scroll down to see other items . I don't know how can I add scrolling action .

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Kinesis said in How to add scroll bar action:

        I don't know how can I add scrolling action

        Like shown here: http://doc.qt.io/qt-5/qscrollarea.html

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        K 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Kinesis said in How to add scroll bar action:

          I don't know how can I add scrolling action

          Like shown here: http://doc.qt.io/qt-5/qscrollarea.html

          K Offline
          K Offline
          Kinesis
          wrote on last edited by
          #5

          @jsulm
          So , can I just add my listwidget on scrollarea ??

          jsulmJ 1 Reply Last reply
          0
          • K Kinesis

            @jsulm
            So , can I just add my listwidget on scrollarea ??

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @Kinesis Sure, you can. Did you try?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            K 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Hi,

              QListWidget already has scrollbars so why do you want to put it in a QScrollArea since it already provides that functionality ?

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

              K 1 Reply Last reply
              3
              • SGaistS SGaist

                Hi,

                QListWidget already has scrollbars so why do you want to put it in a QScrollArea since it already provides that functionality ?

                K Offline
                K Offline
                Kinesis
                wrote on last edited by
                #8

                @SGaist
                If so , how can I use these functionality?

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Kinesis Sure, you can. Did you try?

                  K Offline
                  K Offline
                  Kinesis
                  wrote on last edited by aha_1980
                  #9

                  @jsulm
                  Thanks . I get it now. it's working :)

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    The scroll bars appear as soon as the content exceeds the size of the widget itself, unless you force the policy to always on.

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

                    K 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      The scroll bars appear as soon as the content exceeds the size of the widget itself, unless you force the policy to always on.

                      K Offline
                      K Offline
                      Kinesis
                      wrote on last edited by
                      #11

                      @SGaist
                      Ya, I know that the scroll area will appear like u said . But in my case it wont appear . That's why I need to use scrollarea.

                      1 Reply Last reply
                      1
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        Did you try to determine why they don't appear ?

                        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

                        • Login

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