How to add scroll bar action



  • 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);
            
            
            
        }
    

  • Moderators

    @Kinesis What action do you mean?



  • @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 .


  • Moderators

    @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



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


  • Moderators

    @Kinesis Sure, you can. Did you try?


  • Lifetime Qt Champion

    Hi,

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



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



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


  • Lifetime Qt Champion

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



  • @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.


  • Lifetime Qt Champion

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



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