Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ScrollBar isn't placing at the current position



  • Hi guys ,,
    can anybody please help me to fix this issue ,,
    i upload image below ,,scrollbar automatically shows when the list has too many items but issue is when one user is online or offline, scroll of QListWidget is moving to top ,,
    If I set(drag& drop) scroll to middle and one user become online, scroll is keeping current position ??
    scrollbar has be to set at the setted place only how to do it??
    0_1548444995392_sh.png



  • @J-Hilk
    Thanks for ur solution ,,issue fixed


  • Lifetime Qt Champion

    Hi,

    What are you doing when the user changes its status ?



  • @SGaist thanks for ur reply
    ex:when i set scroll to the middle ,,if some one user comes to the online(already integrated API's to show green colour when user runs our app(below of the image small circle is there know that one)) automatically that scroll goes to the top so ,i want to set it to current postion ??
    just assume u are a 1st person already in online now i ll scroll down scrollbar to middle then im a second person i ll come to the online ,that scrollbar shouldnot move (it should be at the middle only)??


  • Lifetime Qt Champion

    I understood the idea. What I'm asking is what are you doing code wise that makes your scrollbar move when one user changes its status.



  • void ContactsPage::updateContactList()
    {
    ui->list_contacts->clear();

    QList<Contact*> totalList = DataManager::getInstance()->getContacts();
    for(int i = 0; i < totalList.size(); i++)
    {
        Contact* pContact = totalList[i];
        switch(m_activeTab)
        {
            case TCTT_ALL:
                if(pContact->getType() == Contact::TYPE_ACCOUNT)
                {
                    QListWidgetItem* pListWidgetItem = new QListWidgetItem(ui->list_contacts);
                    ui->list_contacts->addItem(pListWidgetItem);
                    AccountCell* pAccountItem = new AccountCell(TCTT_ALL, pContact, this);
                    pListWidgetItem->setSizeHint(QSize(332, 64));
                    ui->list_contacts->setItemWidget(pListWidgetItem, pAccountItem);
    

    i added only these lines of code how do i set scrollbar even i haven't taken scrollbar ,automatically it shows when more items are added ??


  • Moderators

    hi @Dimple

    this is you problem

    ui->list_contacts->clear();

    you completely clear the listwidget and add all items anew, and as a result of that the scrollbar gets reset each time you call updateContactList

    either work with add and remove item or save the scrollbar position before calling clear and set it after wards.

    int tempPos = ui->list_contacts->verticalScrollBar()->value();
    
    ui->list_contacts->clear();
    ....
    ...
    
    ui->list_contacts->verticalScrollBar()->setValue(tempPos);
    
    


  • @J-Hilk
    Thanks for ur solution ,,issue fixed



  • @J-Hilk
    just now i noticed that onemore issue i'am getting as u said if i'am going to add code what happens Now we are refreshing all list items,So, I wanted to update only changed items, not all,
    With current solution, i can go now but if cell count is over than 100, we can feel small delay , so correct solution is to update only changed cells??


  • Moderators

    @Dimple said in ScrollBar isn't placing at the current position:

    With current solution, i can go now but if cell count is over than 100, we can feel small delay , so correct solution is to update only changed cells??

    yes, but that's also more a bandaid as well. For a model that encompasses so many potential items, I would suggest looking into QListView and a proper Delegate&Model for it. Will make your live easier in the long run.



  • @J-Hilk thanks for ur reply
    u mean instead of QListWidget to use QListView ?? i didn't get ??


  • Moderators

    @Dimple

    Yes, QListWidgets is a specialized form of a QListView.

    I you move from from QListWidget to QListView, you'll have to implement your own (data)model and delegate.

    It is more to write/code but will be faster and easier to manage, than using QListWidget and addItem(QListWidgetItem *)



  • can't i do using QListWidget to update only changed cells ?


  • Moderators

    @Dimple
    sure you can, QListWidget offers you functions to remove add individual Items
    for example: http://doc.qt.io/qt-5/qlistwidget.html#takeItem

    I'm just saying, the better way would be to switch to a QListView and a proper model. I know I'm often guilty of using QListWidget myself, as it's easier and faster to create something visual.


Log in to reply