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. QListView & QAbstractListModel remove issue
Forum Updated to NodeBB v4.3 + New Features

QListView & QAbstractListModel remove issue

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.5k Views 1 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.
  • C Offline
    C Offline
    Cannabis2011
    wrote on last edited by
    #1

    I´m experimenting with a simpel contact manager app, but i have an issue where 2 items are deleted instead of 1 item..

    Here are my snippets:

    From mainwindow.cpp:
    @
    if(action->text() == "Delete contact")
    {
    QModelIndex cI = listWidget->currentIndex();
    model->removeContact(cI);
    }
    @

    From Mymodel.cpp (QAbstractListModel):

    @
    void myModel::removeContact(QModelIndex index)
    {
    int i = index.row();
    c.removeAt(i);
    }
    @

    Everytime i click the "Delete button" it removes 2 items instead of 1, is there anything wrong with my code or am i missing something.. It´s a basic QListView with a subclass of QAbstractListModel.

    Edit: Just tell me if you need more snippets.

    Cannabis2011

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      are you sure you don't trigger the method twice?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Cannabis2011
        wrote on last edited by
        #3

        It crossed my mind. When i push the delete button, the app waits a second, then deletes 2 items unless its the last item in the list.. My Logitech G700 has back in time some issues with "Dobbelt click", but i dont think that´s the problem now..

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Cannabis2011
          wrote on last edited by
          #4

          MainWindow.cpp
          @
          #include "mainwindow.h"
          #include <QTreeWidgetItem>

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent)
          {
          //init

          mWidget = new QWidget(this);
          
          QGridLayout *m_Layout = new QGridLayout(this);
          
          listWidget = new QListView(this);
          model = new myModel;
          
          firstNameLine = new QLineEdit(this);
          lastNameLine = new QLineEdit(this);
          ageLine = new QLineEdit(this);
          streetLine = new QLineEdit(this);
          numberLine = new QLineEdit(this);
          postNumber = new QLineEdit(this);
          cityLine = new QLineEdit(this);
          phoneLine = new QLineEdit(this);
          descripBox = new QTextBrowser(this);
          
          //Layouts
          
          m_Layout->addWidget(listWidget,0,0,9,1);
          m_Layout->addWidget(new QLabel("Fornavn:"),0,1);
          m_Layout->addWidget(new QLabel("Efternavn:"),1,1);
          m_Layout->addWidget(new QLabel("Alder:"),2,1);
          m_Layout->addWidget(new QLabel("Gadenavn:"),3,1);
          m_Layout->addWidget(new QLabel("Husnummer:"),4,1);
          m_Layout->addWidget(new QLabel("Postnummer:"),5,1);
          m_Layout->addWidget(new QLabel("By:"),6,1);
          m_Layout->addWidget(new QLabel("Telefon nr.:"),7,1);
          m_Layout->addWidget(new QLabel("Yderligere info:"),8,1);
          
          m_Layout->addWidget(firstNameLine,0,2);
          m_Layout->addWidget(lastNameLine,1,2);
          m_Layout->addWidget(ageLine,2,2);
          m_Layout->addWidget(streetLine,3,2);
          m_Layout->addWidget(numberLine,4,2);
          m_Layout->addWidget(postNumber,5,2);
          m_Layout->addWidget(cityLine,6,2);
          m_Layout->addWidget(phoneLine,7,2);
          m_Layout->addWidget(descripBox,8,2);
          
          m_Layout->setContentsMargins(0,0,0,0);
          
          setCentralWidget(mWidget);
          
          mWidget->setLayout(m_Layout);
          
          //Sizes and settings
          
          int myHeight = height();
          
          setFixedHeight(myHeight);
          
          firstNameLine->setReadOnly(true);
          lastNameLine->setReadOnly(true);
          ageLine->setReadOnly(true);
          streetLine->setReadOnly(true);
          numberLine->setReadOnly(true);
          postNumber->setReadOnly(true);
          cityLine->setReadOnly(true);
          phoneLine->setReadOnly(true);
          
          QMenuBar *mBar = new QMenuBar(this);
          setMenuBar(mBar);
          
          menuBar()->addAction("Add contact");
          menuBar()->addAction("Add to file");
          menuBar()->addAction("Delete contact");
          
          listWidget->setModel(model);
          listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
          listWidget->setRootIndex(QModelIndex());
          
          //Connections
          
          connect(mBar,SIGNAL(triggered(QAction*)),this,SLOT(menuAction(QAction*)));
          connect(listWidget,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(updateView(QModelIndex)));
          
          //Import contacts
          
          QDir dir("contacts\\");
          QFileInfoList fileList = dir.entryInfoList();
          for(int a = 0; a <fileList.count();a++)
          {
              QString info = fileList.at(a).filePath();
              QFile file&#40;info&#41;;
              if(file.open(QIODevice::ReadOnly | QIODevice::Text))
              {
                  QTextStream in(&file);
                  QString fileContent = in.readAll();
                  model->addContact(new contact(fileContent));
              }
          }
          

          }

          MainWindow::~MainWindow()
          {
          }

          void MainWindow::menuAction(QAction *action)
          {
          if(action->text() == "Add contact")
          {
          addDialog *dialog = new addDialog();

              connect(dialog,SIGNAL(buttonClicked(contact*)),this,SLOT(addContact(contact*)));
          
              dialog->show();
          }
          if(action->text() == "Add to file")
          {
              int i = listWidget->currentIndex().row();
              contact *item = model->getContact(i);
              addContactToFile&#40;item&#41;;
          }
          if(action->text() == "Delete contact")
          {
              QModelIndex cI = listWidget->currentIndex();
              contact *rCon = model->getContact(cI.row());
              model->removeContact(rCon);
              QString tempText = QString("%1").arg(cI.row());
              descripBox->setPlainText(tempText);
          }
          

          }

          void MainWindow::updateView(QModelIndex index)
          {
          int i = index.row();
          contact *item = model->getContact(i);

          firstNameLine->setText(item->ForNavn());
          lastNameLine->setText(item->EfterNavn());
          ageLine->setText(item->Alder());
          streetLine->setText(item->GadeNavn());
          numberLine->setText(item->HusNummer());
          postNumber->setText(item->PostNummer());
          cityLine->setText(item->By());
          phoneLine->setText(item->Tlf());
          descripBox->setText(item->Info());
          

          }

          void MainWindow::clearBoxes()
          {
          firstNameLine->clear();
          lastNameLine->clear();
          ageLine->clear();
          streetLine->clear();
          numberLine->clear();
          postNumber->clear();
          cityLine->clear();
          phoneLine->clear();
          descripBox->clear();
          }

          void MainWindow::addContact(contact *c)
          {
          model->addContact(c);
          }

          void MainWindow::addContactToFile(contact *toFile)
          {
          QStringList toStrings = toFile->allStrings();
          QString string = toFile->KaldeNavn();
          QFile file(QString("contacts\%1.txt").arg(string));
          if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
          {
          return;
          }
          else
          {
          for(int a = 0;toStrings.count();a++)
          {
          QTextStream out(&file);
          out << toStrings.at(a) << "\r\n";
          }
          }
          file.close();
          }
          @

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Cannabis2011
            wrote on last edited by
            #5

            Model.cpp

            @
            #include "mymodel.h"

            myModel::myModel(QObject *parent) :
            QAbstractListModel(parent)
            {
            }

            QVariant myModel::data(const QModelIndex &index, int role) const
            {
            if(!index.isValid())
            return QVariant();

            if(index.row() > c.count() && index.row() <0)
                return QVariant();
            
            if(role != Qt::DisplayRole)
                return QVariant();
            
            int row = index.row();
            
            QVariant d = c.at(row)->KaldeNavn();
            
            return d;
            

            }

            int myModel::rowCount(const QModelIndex &parent) const
            {
            Q_UNUSED(parent);

            return c.count();
            

            }

            void myModel::addContact(contact *C)
            {
            beginInsertRows(QModelIndex(),0,0);
            c.append(C);
            endInsertRows();
            }

            void myModel::removeContact(contact *rCon)
            {
            for(int a = 0; a < c.count();a++)
            {
            if(rCon->KaldeNavn() == c.at(a)->KaldeNavn())
            {
            c.removeAt(a);
            }
            }
            }

            contact * const myModel::getContactByName(QString name)
            {
            contact *item;

            for(int a = 0; a <c.count();a++)
                {
                    if(name == c.at(a)->KaldeNavn())
                        {
                            item = c.at(a);
                        }
                }
            return item;
            

            }

            const QStringList myModel::getAllData(int i)
            {
            return c.at(i)->allStrings();
            }
            @

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Cannabis2011
              wrote on last edited by
              #6

              I solved the problem by replacing QMenuBar with som QPushButtons.. I don´t know, but somehow the button in the menubar must have emitted 2 signals..

              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