Doubt on MVC and indexes



  • Hello, i've been using MVC for some time now and i have some doubts. I Really would love some enlightment here as i'm not a Qt Genius but a qt lover.

    I'm extending QAbstractTableModel and everythings works perfect.

    But all indexes i create and receive (ie in the data function ) identifies the item by it's column/row (internally).
    Internally i identify most of my entities by some kind of id, but still i must convert everything to column/row, that means internally i must asure some kind of order. (at least for the session) And in the worst case i must make a linear search to know which row is which id.

    Also, i have composite classes, ie:
    a product has a source. that means when i try to relate them both, i use the source id , which is stored in the product
    but if i use a delegate in the product list i find it hard to relate them both, specially if in the combobox ,i want to show another column than the source id (and of course storing the "index" (row/column based) in the product would make no sense)

    So far i've solved it by using the UserRole (returning the source id) which allowed me to use a delegate for the source in the product list, and also use a QSortFilterProxyModel
    Now i must use findData to know the index of a item, looks not very efficient, but i don't know how to do it unless not using column/row indexes.

    Also i feel is not as smooth as everything else in Qt so i was wondering if there's another way to work with model indexes rather than a column/row representation. (I've read about PersistentIndex but i'm not sure it's the one i want)

    Also sometimes i need to mix table-models with simple list-views (ie comboboxes) i dont know if persistent indexes works fine there.

    Thanks a lot for any response.


  • Moderators

    Why don't you use QModelIndex::internal(Id|Pointer) to store your id, passing it around with your model indices?



  • i've used the 1st option in this doc http://qt-project.org/faq/answer/how_can_a_qmodelindex_be_retrived_from_the_model_for_an_internal_data_item

    how do i work with that? can you have a link to the relevant part in documentation?

    can i return a pointer to the actual entity object with that?


  • Moderators

    Does that link even apply? That is for finding the QModelIndex for an internal item while we are discussing finding the internal data item from a QModelIndex. I hope I did not misunderstand your initial post.

    QAbstractItemModel has a protected createIndex method that you need to use to create all your indices. That accepts a void pointer which will then be available as internalPointer() on the QModelIndex it creates. Just check the documentation on QAbstractItemModel. I am sure you will find the relevant sections.



  • I'm not exactly discussing QModelIndex, i just want a better way to reference items in my models instead of depending of column/row-based indexes..
    i have no idea how to work otherway.
    I suppose i can do that creating my own QModelIndexes.
    Should i implement my own _QAbstractItemModel::index _and call createIndex with a value for the ptr param?



  • Qt is giving the following error in my code "invalid use of incomplete type struct ui::calculator" . These are my files below :
    calculator.h

    @#ifndef CALCULATOR_H
    #define CALCULATOR_H

    #include <QMainWindow>

    namespace Ui {
    class calculator;
    }

    class calculator : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit calculator(QWidget *parent = 0);
    ~calculator();

    private:
    Ui::calculator *ui;

    private :
    QString str;
    QString str1;
    QString ch;
    QString strResult;
    QString text1;

    public:
    float num;
    float num1;
    float ans;

    private slots:
    void add();
    void subtract();
    void multiply();
    void divide();
    void equal();
    void one();
    void two();
    void three();
    void four();
    void five();
    void six();
    void seven();
    void eight();
    void nine();
    void zero();
    void on();
    void off();
    void reset();
    void dot();

    };
    #endif // CALCULATOR_H
    @
    Please reply as soon as possible . As I am a beginner in QT I don't have enough experience. Thanx in advance.



  • calculator.cpp
    calculator.cpp

    @#include "calculator.h"
    #include "ui_calculator.h"

    calculator::calculator(QWidget *parent): QMainWindow(parent), ui(new Ui::calculator)
    {
    ui->setupUi(this);
    text1="";
    ui->line->setAlignment(Qt::AlignRight);
    ui->line->setMaxLength(12);
    ans=0;

           QObject::connect(ui->but1,SIGNAL(clicked()),this,SLOT(add()));
           QObject::connect(ui->but2,SIGNAL(clicked()),this,SLOT(subtract()));
           QObject::connect(ui->but3,SIGNAL(clicked()),this,SLOT(multiply()));
           QObject::connect(ui->but4,SIGNAL(clicked()),this,SLOT(divide()));
           QObject::connect(ui->equal,SIGNAL(clicked()),this,SLOT(equal()));
           QObject::connect(ui->one,SIGNAL(clicked()),this,SLOT(one()));
           QObject::connect(ui->two,SIGNAL(clicked()),this,SLOT(two()));
           QObject::connect(ui->three,SIGNAL(clicked()),this,SLOT(three()));
           QObject::connect(ui->four,SIGNAL(clicked()),this,SLOT(four()));
           QObject::connect(ui->five,SIGNAL(clicked()),this,SLOT(five()));
           QObject::connect(ui->six,SIGNAL(clicked()),this,SLOT(six()));
           QObject::connect(ui->seven,SIGNAL(clicked()),this,SLOT(seven()));
           QObject::connect(ui->eight,SIGNAL(clicked()),this,SLOT(eight()));
           QObject::connect(ui->nine,SIGNAL(clicked()),this,SLOT(nine()));
           QObject::connect(ui->zero,SIGNAL(clicked()),this,SLOT(zero()));
           QObject::connect(ui->reset,SIGNAL(clicked()),this,SLOT(reset()));
           QObject::connect(ui->on,SIGNAL(clicked()),this,SLOT(on()));
           QObject::connect(ui->off,SIGNAL(clicked()),this,SLOT(off()));
           QObject::connect(ui->dot,SIGNAL(clicked()),this,SLOT(dot()));
    

    }

    calculator::~calculator()
    {
    delete ui;
    }

    void calculator::dot()
    {
    text1+=ui->dot->text();
    ui->line->setText(text1);
    }
    void calculator::off()
    {
    ui->one->setEnabled(0);
    ui->two->setEnabled(0);
    ui->three->setEnabled(0);
    ui->four->setEnabled(0);
    ui->five->setEnabled(0);
    ui->six->setEnabled(0);
    ui->seven->setEnabled(0);
    ui->eight->setEnabled(0);
    ui->nine->setEnabled(0);
    ui->zero->setEnabled(0);
    ui->equal->setEnabled(0);
    ui->but1->setEnabled(0);
    ui->but2->setEnabled(0);
    ui->but3->setEnabled(0);
    ui->but4->setEnabled(0);
    ui->off->setEnabled(0);
    ui->reset->setEnabled(0);
    ui->on->setEnabled(1);
    ui->line->setEnabled(0);
    ui->dot->setEnabled(0);
    //ui->backspc->setEnabled(0);
    ui->line->clear();
    }
    void calculator::on()
    {
    ui->one->setEnabled(1);
    ui->two->setEnabled(1);
    ui->three->setEnabled(1);
    ui->four->setEnabled(1);
    ui->five->setEnabled(1);
    ui->six->setEnabled(1);
    ui->seven->setEnabled(1);
    ui->eight->setEnabled(1);
    ui->nine->setEnabled(1);
    ui->zero->setEnabled(1);
    ui->equal->setEnabled(1);
    ui->but1->setEnabled(1);
    ui->but2->setEnabled(1);
    ui->but3->setEnabled(1);
    ui->but4->setEnabled(1);
    ui->off->setEnabled(1);
    ui->reset->setEnabled(1);
    ui->on->setEnabled(0);
    ui->line->setEnabled(1);
    ui->dot->setEnabled(1);
    //ui->backspc->setEnabled(1);

    }
    void calculator::reset()
    {
    text1="";
    ui->line->clear();
    }
    void calculator::one()
    {

    text1+=ui->one->text();
    ui->line->setText(text1);
    

    }
    void calculator::two()
    {

    text1+=ui->two->text();
    ui->line->setText(text1);
    

    }
    void calculator::three()
    {

    text1+=ui->three->text();
    ui->line->setText(text1);
    

    }
    void calculator::four()
    {

    text1+=ui->four->text();
    ui->line->setText(text1);
    

    }
    void calculator::five()
    {

    text1+=ui->five->text();
    ui->line->setText(text1);
    

    }
    void calculator::six()
    {

    text1+=ui->six->text();
    ui->line->setText(text1);
    

    }
    void calculator::seven()
    {
    text1+=ui->seven->text();
    ui->line->setText(text1);

    }
    void calculator::eight()
    {
    text1+=ui->eight->text();
    ui->line->setText(text1);

    }
    void calculator::nine()
    {
    text1+=ui->nine->text();
    ui->line->setText(text1);
    }
    void calculator::zero()
    {

    text1+=ui->zero->text();
    ui->line->setText(text1);
    

    }
    void calculator::add() //Addition
    {
    str=ui->line->text();
    num=str.toFloat();
    ch=ui->but1->text();
    ui->line->clear();
    text1="";
    }
    void calculator::subtract() //Subtraction
    {
    str=ui->line->text();
    num=str.toFloat();
    ch=ui->but2->text();
    ui->line->clear();
    text1="";
    }
    void calculator::multiply() //Multiplication
    {
    str=ui->line->text();
    num=str.toFloat();
    ch=ui->but3->text();
    ui->line->clear();
    text1="";
    }
    void calculator::divide() //Division
    {
    str=ui->line->text();
    num=str.toFloat();
    ch=ui->but4->text();
    ui->line->clear();
    text1="";
    }
    void calculator::equal()
    {
    str1 = ui->line->text();
    num1 = str1.toFloat();
    if (ch=="+")
    {
    ans=num+num1;
    }
    else if(ch=="-")
    {
    ans=num-num1;
    }
    else if(ch=="")
    {
    ans=num
    num1;
    }
    else if(ch=="/")
    {
    ans=num/num1;
    }
    else
    {
    strResult = strResult.number(ans);
    ui->line->setText(strResult);
    }
    strResult = strResult.number(ans);
    ui->line->setText(strResult);
    ch="";
    text1=strResult;
    }
    @



  • @Mayank

    Welcome to devnet, as you are new I would like to say that you posted your question at the wrong place as its completely different from the thread topic. In case you have any question you can start the discussion by going to

    Forums --> Start New Discussion

    For you calculator program you can have a look at a "similar example":http://developer.nokia.com/Community/Wiki/How_to_make_a_simple_calculator_in_Qt

    Regards
    Sam



  • @ Sam
    thanx!!! :)


Log in to reply
 

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