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. Problem using QVector through classes.
Forum Updated to NodeBB v4.3 + New Features

Problem using QVector through classes.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.2k 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.
  • D Offline
    D Offline
    Ducbr
    wrote on last edited by
    #1

    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.cpp
    InsertDate::InsertDate(QTableWidget table, QVector<QDate> vec_dates, QWidget *parent)
    :QWidget(parent)
    {
    this->dates=vec_dates;
    this->tabla=table;

    BuildWindow();
    connect(Guardar,SIGNAL(clicked()),this,SLOT(SaveDate()));
    connect(Cancelar,SIGNAL(clicked()),this,SLOT(close()));
    

    }
    InsertDate::~InsertDate(){
    delete this;
    }

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

    bool valido = QDate::isValid(yyyy->value(),mm->value(),dd->value());
    if(!valido){
        QMessageBox mes;
        mes.warning(0,QString("Error"),QString("La fecha no es valida"));
    }else{
        QDate *dateis = new QDate(yyyy->value(),mm->value(),dd->value());
        tabla->setRowCount(aux+1);
        tabla->setItem(aux,0,new QTableWidgetItem(dateis->toString("dd/MM/yyyy")));
        //tabla->setItem(aux,1,new QTableWidgetItem(mm->text()));
        //vec_dates.insert(vec_dates.size(),dateis);
    
        this->close();
    }
    

    }
    @

    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:

    @
    connect(Guardar,SIGNAL(clicked()),this,SLOT(SaveDate(QVector<QDate*>)));
    ...
    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:

    @InsertDate.h
    class InsertDate : public QWidget
    {
    Q_OBJECT
    public:
    explicit InsertDate(QTableWidget table, QVector<QDate> vec_dates, QWidget *parent = 0);
    ~InsertDate();
    void BuildWindow();

    public slots:
    void SaveDate();

    private:
    QVector<QDate*> dates;
    QTableWidget *tabla;

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

    };
    @

    @
    calendar.h
    ....
    public:
    QVector<QDate*> holidays;
    ...

    calendar.cpp
    void calendar:: AddHoliday()
    {
    insertWindow = new InsertDate(ui->FesTable,holidays);
    insertWindow->show();
    }

    @

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

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      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.
      1 Reply Last reply
      0
      • D Offline
        D Offline
        Ducbr
        wrote on last edited by
        #3

        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 =)

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Ducbr
          wrote on last edited by
          #4

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

          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