Important: Please read the Qt Code of Conduct -

Problem using QVector through classes.

  • Hi there!
    First of all thanks for this web, I just started developing in QT few weeks ago and it has helped me really much. But now I'm dealing with a really disturbing problem concerning QVector, so I decided signing up and see if anyone could help me. The problem is in the following Code:

    InsertDate::InsertDate(QTableWidget table, QVector<QDate> vec_dates, QWidget *parent)


    delete this;

    void InsertDate:: SaveDate(){
    int aux = tabla->rowCount();

    bool valido = QDate::isValid(yyyy->value(),mm->value(),dd->value());
        QMessageBox mes;
        mes.warning(0,QString("Error"),QString("La fecha no es valida"));
        QDate *dateis = new QDate(yyyy->value(),mm->value(),dd->value());
        tabla->setItem(aux,0,new QTableWidgetItem(dateis->toString("dd/MM/yyyy")));
        //tabla->setItem(aux,1,new QTableWidgetItem(mm->text()));


    As you can see i'm trying to modify a Vector of QDates. I've been using QVector to create Spins in other files and worked as expected, but here i can't modify it. The problem then is that when i try to modify dates inserting a QDate it doesn't modify the vec_dates QVector, so I tried using this:

    void InsertDate:: SaveDate(QVector<Qdate*> vec_dates){...}

    but it crashes.
    I've got no idea left of how to do it and I really need to understand why it is not working,

    If needed, here are the headers:

    class InsertDate : public QWidget
    explicit InsertDate(QTableWidget table, QVector<QDate> vec_dates, QWidget *parent = 0);
    void BuildWindow();

    public slots:
    void SaveDate();

    QVector<QDate*> dates;
    QTableWidget *tabla;

    QHBoxLayout *HBox;
    QSpinBox *dd;    QSpinBox *mm;    QSpinBox *yyyy;
    QPushButton *Guardar;    QPushButton *Cancelar;


    QVector<QDate*> holidays;

    void calendar:: AddHoliday()
    insertWindow = new InsertDate(ui->FesTable,holidays);


    I beg your help. Thanks, and if any code you need to understand it is missing just say and i'll upload it asap.

  • You're never modifying the dates vector, how should the dates appear in it then? Your saveDates function should have something like dates->append(dateis);
    //EDIT: Ah yes, and you're creating a copy of a vector to date pointers. But you actually want a pointer to a vector of dates. so QVector<QDate> *dates;
    Because it's the date vector you want to modify, not each individual date only.

    A few remarks:

    • Program in english, when it's an english programming language. Don't mix languages by bringing in non-english variable names.
    • Why not use a QVector<QDate>? I don't see a benefit using pointers to dates here, only more chances to do mistakes.
    • A method called "saveDate" shouldn't call this->quit();. That's unexpected behaviour and will confuse other programmers and probably the future you. At least call it "saveAndClose", or "acceptDate" or whatever.

  • Hi thanks! Maybe that's where my problem with the pointers, i'll try it now. And yes, I program in english, the problem is my partner in this project doesn't so she complains me everytime about just using english because doesn't understand it. Thanks for the advice anyway =)

  • Well now i can say it's defenetly solved.
    Thanks mate =)

Log in to reply