OK, I am creating a simple program just to test out what I've learned. I have successfully created a very simple list model and it works correctly with the table view control. I'm now trying to add functionality to the menu items that allow the user to manipulate the items in the table. Upon first testing it, it was clear that the slot method had not run, even though I connected the QAction signal to the said slot. It then occurred to me that I had not yet added Q_OBJECT to the class declaration of my window class. I was relieved when I realized this, but after I added the macro and tried to compile it using Qt Creator, it generated errors. I can't seem to figure out what the problem is, but the error messages are pointing me to source files I Have no business in and to my constructor declarations in my cpp files.
I really can't figure out what the problem is, so I'm just going to post the whole thing up here and hope someone has the solution. If it were an algorithmic or syntax blunder, no doubt I would diligently look into it. But I cannot identify what this is.
#include <QtGui>
#include "AppWnd.h"
int main(int argc, char** argv)
{
AppWnd appWnd(0);
QObject::connect(&appWnd,
SIGNAL(close
()),
&app,
SLOT(quit
()));
return app.exec();
}
#include <QtGui>
#include "AppWnd.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
AppWnd appWnd(0);
QObject::connect(&appWnd, SIGNAL(close()), &app, SLOT(quit()));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
#ifndef GMODEL_H
#define GMODEL_H
#include <QtGui>
#include <QList>
#include <QPair>
{
Q_OBJECT //No problems with this one just to let you know. It works!
private:
public:
~GModel();
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const;
};
#endif // GMODEL_H
#ifndef GMODEL_H
#define GMODEL_H
#include <QtGui>
#include <QList>
#include <QPair>
class GModel : public QAbstractListModel
{
Q_OBJECT //No problems with this one just to let you know. It works!
QPixmap *m;
private:
QStringList pl;
QString extractName(QString &string) const;
QString extractAge(QString &string) const;
public:
GModel(QObject *parent);
~GModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
};
#endif // GMODEL_H
To copy to clipboard, switch view to plain text mode
#include "GModel.h"
{
m
->fill
(QColor(0,
0,
255,
255));
pl.
append(QString("Joe Skurk, 23"));
pl.
append(QString("John Skelp, 26"));
pl.
append(QString("Flim Barbson, 21"));
pl.
append(QString("Flark Timberly, 43"));
pl.
append(QString("Jespit Vonvooloo, 15"));
pl.
append(QString("Norman Walker, 13"));
}
GModel::~GModel()
{
delete m;
}
QVariant GModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if (role
!= Qt
::DisplayRole) return QVariant();
if (orientation == Qt::Vertical)
{
}
if (orientation == Qt::Horizontal)
{
switch(section)
{
case 0:
break;
case 1:
break;
}
}
}
{
return pl.count();
}
int GModel
::columnCount(const QModelIndex &parent
) const {
return 2;
}
{
if (!index.
isValid()) return QVariant();
if (index.
row() >
= pl.
count()) return QVariant();
if (role == Qt::DisplayRole)
{
switch (index.column())
{
case 0:
return extractName(data);
break;
case 1:
return extractAge(data);
break;
}
}
}
{
return string.left(string.indexOf(",")).trimmed();
}
{
return string.mid(string.indexOf(",")+1, string.length() - string.indexOf(",")+1);
}
{
return createIndex(row, column, 0);
}
{
if (index.isValid() && role == Qt::EditRole)
{
switch(index.column())
{
case 0:
pl.
replace(index.
row(), value.
toString() + QString(", ") + age
);
break;
case 1:
pl.
replace(index.
row(), name
+ QString(", ") + value.
toString());
break;
}
emit dataChanged(index, index);
return true;
}
return false;
}
#include "GModel.h"
GModel::GModel(QObject *parent) : QAbstractListModel(parent)
{
m = new QPixmap(20, 20);
m->fill(QColor(0, 0, 255, 255));
pl.append(QString("Joe Skurk, 23"));
pl.append(QString("John Skelp, 26"));
pl.append(QString("Flim Barbson, 21"));
pl.append(QString("Flark Timberly, 43"));
pl.append(QString("Jespit Vonvooloo, 15"));
pl.append(QString("Norman Walker, 13"));
}
GModel::~GModel()
{
delete m;
}
QVariant GModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) return QVariant();
if (orientation == Qt::Vertical)
{
return QVariant();
}
if (orientation == Qt::Horizontal)
{
switch(section)
{
case 0:
return QString("Name");
break;
case 1:
return QString("Age");
break;
}
}
return QVariant();
}
int GModel::rowCount(const QModelIndex &parent) const
{
return pl.count();
}
int GModel::columnCount(const QModelIndex &parent) const
{
return 2;
}
QVariant GModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
if (index.row() >= pl.count()) return QVariant();
if (role == Qt::DisplayRole)
{
QString data = pl.at(index.row());
switch (index.column())
{
case 0:
return extractName(data);
break;
case 1:
return extractAge(data);
break;
}
}
return QVariant();
}
QString GModel::extractName(QString &string) const
{
return string.left(string.indexOf(",")).trimmed();
}
QString GModel::extractAge(QString &string) const
{
return string.mid(string.indexOf(",")+1, string.length() - string.indexOf(",")+1);
}
QModelIndex GModel::index(int row, int column, const QModelIndex &parent) const
{
return createIndex(row, column, 0);
}
bool GModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole)
{
QString data = pl.at(index.row());
QString name = extractName(data);
QString age = extractAge(data);
switch(index.column())
{
case 0:
pl.replace(index.row(), value.toString() + QString(", ") + age);
break;
case 1:
pl.replace(index.row(), name + QString(", ") + value.toString());
break;
}
emit dataChanged(index, index);
return true;
}
return false;
}
To copy to clipboard, switch view to plain text mode
#ifndef APPWND_H
#define APPWND_H
#include <QtGui>
#include "GModel.h"
{
Q_OBJECT
private:
GModel *gm;
public:
//void setGModel(GModel *gmodel);
public slots:
void createPerson();
void deletePerson();
void changeAge();
void changeName();
};
#endif // APPWND_H
#ifndef APPWND_H
#define APPWND_H
#include <QtGui>
#include "GModel.h"
class AppWnd : public QMainWindow
{
Q_OBJECT
private:
GModel *gm;
public:
AppWnd(QWidget *parent = 0);
//void setGModel(GModel *gmodel);
public slots:
void createPerson();
void deletePerson();
void changeAge();
void changeName();
};
#endif // APPWND_H
To copy to clipboard, switch view to plain text mode
#include "AppWnd.h"
#include "AddPersonDialog.h"
#include <QtGui>
{
setWindowTitle("Model/View Testing");
setFixedSize(400, 500);
gm = new GModel(0);
lv->setModel(gm);
//lv->resize(380, 480);
//lv->move(10, 10);
QMenu *peopleMenu
= mb
->addMenu
("People");
QAction *a_createPerson
= peopleMenu
->addAction
("Create Person",
this,
SLOT(createPerson
()));
QAction *a_deletePerson
= peopleMenu
->addAction
("Delete Person",
this,
SLOT(deletePerson
()));
peopleMenu->addSeparator();
QAction *a_changeName
= peopleMenu
->addAction
("Change Name",
this,
SLOT(changeName
()));
QAction *a_changeAge
= peopleMenu
->addAction
("Change Age",
this,
SLOT(changeAge
()));
setCentralWidget(lv);
show();
}
void AppWnd::createPerson()
{
AddPersonDialog dlg(this);
dlg.exec();
}
void AppWnd::deletePerson()
{
}
void AppWnd::changeName()
{
}
void AppWnd::changeAge()
{
}
/*void AppWnd::setGModel(GModel *gmodel)
{
gm = gmodel;
}*/
#include "AppWnd.h"
#include "AddPersonDialog.h"
#include <QtGui>
AppWnd::AppWnd(QWidget *parent) : QMainWindow(parent)
{
setWindowTitle("Model/View Testing");
setFixedSize(400, 500);
QTableView *lv = new QTableView(this);
gm = new GModel(0);
lv->setModel(gm);
//lv->resize(380, 480);
//lv->move(10, 10);
gm->setData(gm->index(0, 1, QModelIndex()), QString("50"), Qt::EditRole);
QMenuBar *mb = menuBar();
QMenu *peopleMenu = mb->addMenu("People");
QAction *a_createPerson = peopleMenu->addAction("Create Person", this, SLOT(createPerson()));
QAction *a_deletePerson = peopleMenu->addAction("Delete Person", this, SLOT(deletePerson()));
peopleMenu->addSeparator();
QAction *a_changeName = peopleMenu->addAction("Change Name", this, SLOT(changeName()));
QAction *a_changeAge = peopleMenu->addAction("Change Age", this, SLOT(changeAge()));
setCentralWidget(lv);
show();
}
void AppWnd::createPerson()
{
AddPersonDialog dlg(this);
dlg.exec();
}
void AppWnd::deletePerson()
{
}
void AppWnd::changeName()
{
}
void AppWnd::changeAge()
{
}
/*void AppWnd::setGModel(GModel *gmodel)
{
gm = gmodel;
}*/
To copy to clipboard, switch view to plain text mode
#ifndef ADDPERSONDIALOG_H
#define ADDPERSONDIALOG_H
#include <QtGui>
class AddPersonDialog
: public QDialog{
Q_OBJECT
private:
public:
AddPersonDialog
(QWidget *parent
= 0);
};
#endif // ADDPERSONDIALOG_H
#ifndef ADDPERSONDIALOG_H
#define ADDPERSONDIALOG_H
#include <QtGui>
class AddPersonDialog : public QDialog
{
Q_OBJECT
private:
QLineEdit *name;
QLineEdit *age;
public:
AddPersonDialog(QWidget *parent = 0);
QString getName();
QString getAge();
};
#endif // ADDPERSONDIALOG_H
To copy to clipboard, switch view to plain text mode
#include "AddPersonDialog.h"
AddPersonDialog
::AddPersonDialog(QWidget *parent
) : QDialog(parent,
0){
hbl->addWidget(ok);
hbl->addWidget(cancel);
hbl2->addWidget(name);
hbl3->addWidget(age);
vbl->addLayout(hbl2);
vbl->addLayout(hbl3);
vbl->addLayout(hbl);
setLayout(vbl);
QObject::connect(ok,
SIGNAL(clicked
()),
this,
SLOT(accept
()));
QObject::connect(cancel,
SIGNAL(clicked
()),
this,
SLOT(reject
()));
}
{
return name->text();
}
{
return age->text();
}
#include "AddPersonDialog.h"
AddPersonDialog::AddPersonDialog(QWidget *parent) : QDialog(parent, 0)
{
QPushButton *ok = new QPushButton("OK");
QPushButton *cancel = new QPushButton("Cancel");
name = new QLineEdit();
age = new QLineEdit();
QHBoxLayout *hbl = new QHBoxLayout();
hbl->addWidget(ok);
hbl->addWidget(cancel);
QHBoxLayout *hbl2 = new QHBoxLayout();
hbl2->addWidget(name);
QHBoxLayout *hbl3 = new QHBoxLayout();
hbl3->addWidget(age);
QVBoxLayout *vbl = new QVBoxLayout();
vbl->addLayout(hbl2);
vbl->addLayout(hbl3);
vbl->addLayout(hbl);
setLayout(vbl);
QObject::connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
QObject::connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
}
QString AddPersonDialog::getName()
{
return name->text();
}
QString AddPersonDialog::getAge()
{
return age->text();
}
To copy to clipboard, switch view to plain text mode
Bookmarks