Problem using QVector through classes.
-
wrote on 2 Dec 2012, 11:44 last edited by
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.
-
wrote on 2 Dec 2012, 12:30 last edited by
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.
-
wrote on 2 Dec 2012, 12:39 last edited by
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 =)
-
wrote on 2 Dec 2012, 17:58 last edited by
Well now i can say it's defenetly solved.
Thanks mate =)
1/4