How to use Qlist to create a user profile model with about 15 different fields - qt

I know some professional developers are shaking there heads with disappointment... Im just trying to wrap my head around how to use Qlist to store multiple types of data within one table. And I know I will use QTableModel to set this data right? Building a billing app. Basic ideas or links will be fine... Thanks guys

Here is a good read for the best practices for Model/View Programming in Qt.
The examples and links at the bottom of the above link are nice, too.
As for storing multiple types, I would look at QVariant and QSettings; using QVariant is referenced many times in the Model/View Programming in Qt.
QVariant is very robust and works well with many different types. It is core in reading values out of QSettings. Be sure to read the description of QSettings for more ideas.
QBitArray toBitArray () const
bool toBool () const
QByteArray toByteArray () const
QChar toChar () const
QDate toDate () const
QDateTime toDateTime () const
double toDouble ( bool * ok = 0 ) const
QEasingCurve toEasingCurve () const
float toFloat ( bool * ok = 0 ) const
QHash<QString, QVariant> toHash () const
int toInt ( bool * ok = 0 ) const
QLine toLine () const
QLineF toLineF () const
QList<QVariant> toList () const
QLocale toLocale () const
qlonglong toLongLong ( bool * ok = 0 ) const
QMap<QString, QVariant> toMap () const
QPoint toPoint () const
QPointF toPointF () const
qreal toReal ( bool * ok = 0 ) const
QRect toRect () const
QRectF toRectF () const
QRegExp toRegExp () const
QSize toSize () const
QSizeF toSizeF () const
QString toString () const
QStringList toStringList () const
QTime toTime () const
uint toUInt ( bool * ok = 0 ) const
qulonglong toULongLong ( bool * ok = 0 ) const
QUrl toUrl () const
Another good option is QString. It has a number of built in conversions also:
QByteArray toAscii () const
QString toCaseFolded () const
double toDouble ( bool * ok = 0 ) const
float toFloat ( bool * ok = 0 ) const
int toInt ( bool * ok = 0, int base = 10 ) const
QByteArray toLatin1 () const
QByteArray toLocal8Bit () const
long toLong ( bool * ok = 0, int base = 10 ) const
qlonglong toLongLong ( bool * ok = 0, int base = 10 ) const
QString toLower () const
short toShort ( bool * ok = 0, int base = 10 ) const
std::string toStdString () const
std::wstring toStdWString () const
uint toUInt ( bool * ok = 0, int base = 10 ) const
ulong toULong ( bool * ok = 0, int base = 10 ) const
qulonglong toULongLong ( bool * ok = 0, int base = 10 ) const
ushort toUShort ( bool * ok = 0, int base = 10 ) const
QVector<uint> toUcs4 () const
QString toUpper () const
QByteArray toUtf8 () const
int toWCharArray ( wchar_t * array ) const
When saving numerical values to a QString, be sure to use QString::number().

Related

Convert text in other encoding qt

Alert: I am using QByteArray.
I want to ask about which other conversions exist. I am normally using toLatin1 but i would try with others. Example:
datoss = "|#|" + ui->textocuenta->text().toLatin1() + "|#|";
I repeat again, i am trying to use other conversion. Only that.
See QString class info. Convert your QByteArray to QString, there are multiple conversions:
CFStringRef toCFString() const
QString toCaseFolded() const
QString toHtmlEscaped() const
QByteArray toLatin1() const
QByteArray toLocal8Bit() const
NSString * toNSString() const
std::string toStdString() const
std::u16string toStdU16String() const
std::u32string toStdU32String() const
std::wstring toStdWString() const
ushort toUShort(bool *ok = Q_NULLPTR, int base = 10) const
QVector<uint> toUcs4() const
QByteArray toUtf8() const
int toWCharArray(wchar_t *array) const
If you are looking for a way to convert your indexed data from your QWidget in to a QByteArray, just use:
const QString indexed = QString("|#|%1|#|").arg(ui->textocuenta->text());
datoss = indexed.toLatin1();

using QString as argmuent in SLOT macro

Is it possible to be used as an argument QString in SLOT macro?
PS. I mean a simple solution .. Not as in QMetaObject::connectSlotsByName().
No you cannot pass QString to SLOT macro. But you can use QString for connect. Also connect cannot take QString, so you have to convert it to const char *. Simple example is:
QString slotName = SLOT(clicked());
connect(ui->pushButton, SIGNAL(clicked()), qPrintable(slotName));
SLOT just stringifies passed parameter and concatenate it with 1:
# define SLOT(a) qFlagLocation("1"#a QLOCATION)
If you don't want to use SLOT it's possible to write code like this:
QString slotName = QString::number(QSLOT_CODE) + "clicked()";
This is raw code from my current project. It parses chat commands like /me customtext and calls cmd_me( const QString& params ); slot.
To introduce new command it's enough to create private slot with void cmd_*( const QString& ); signature.
Here is code:
void ConsoleController::onCommand( const QString& cmd )
{
if ( cmd.length() < 1 )
return ;
if ( cmd[0] != '/' )
return ;
const QMetaObject *meta = metaObject();
int posParam = cmd.indexOf( ' ' );
if ( posParam == -1 )
posParam = cmd.length();
const QString command = cmd.mid( 1, posParam - 1 ).toLower();
const QString params = cmd.mid( posParam + 1 );
const QString slotName = QString( "cmd_%1( const QString& )" ).arg( command );
const QString normalizedSlotName = QMetaObject::normalizedSignature( slotName.toStdString().c_str() );
for ( int i = 0; i < meta->methodCount(); i++ )
{
QMetaMethod method = meta->method( i );
if ( method.methodType() != QMetaMethod::Slot )
continue;
const QString signature = method.signature();
if ( normalizedSlotName == signature )
{
method.invoke( this, Q_ARG( QString, params ) );
return ;
}
}
log( QString( "Command \"%1\" not recognized, type /help to list all available commands" ).arg( command ) );
}
You can take an idea and adapt it for your needs.

Using QList as the input for QwtPlotCurves setSamples

I have two QList (XList, YList) which saves the dynamic data. I want to use these as the input of the QwtPlotCurves by setSamples. After I check the documentation:
void setSamples (const double *xData, const double *yData, int size)
void setSamples (const QVector< double > &xData, const QVector< double > &yData)
void setSamples (const QVector< QPointF > &)
It seems do not support QList. Is there workaround to this or do I have to overload it?
Julio
There is method in QList that returns a const QVector.
So:
setSamples( XList.toVector(), YList.toVector() )
Check QVector QList::toVector () const

Declare const pointer to int?

In C++ we have the following:
int* p1; // pointer to int
const int* p2; // pointer to constant int
int* const p3; // constant pointer to int
const int* const p4; // constant pointer to constant int
and in D:
int* p1; // pointer to int
const(int)* p2; // pointer to constant int
?? ?? ?? // constant pointer to int
const(int*) p4; // constant pointer to constant int
what's the syntax for constant pointer to int?
D does not have head const.
I think you can simulate it:
struct Ptr(T)
{
T* _val;
this(T* nval) const
{
_val = nval;
}
#property T* opCall() const
{
return cast(T*)_val;
}
alias opCall this;
}
void main()
{
int x = 1;
int y = 2;
const Ptr!int ptrInt = &x;
assert(*ptrInt == 1);
*ptrInt = y; // ok
assert(*ptrInt == 2);
assert(x == 2);
ptrInt = &y; // won't compile, good.
}

In QT, chaining models does not work as expected

Alright, I have a really basic QStandardItemModel, filled with some numbers. I managed to display it in a QTableView, it's ok. I created a new model ( subclass either of QAbstractItemModel or QAbstractProxyModel ), which is some kind of a layer of an existing model - it is needed to set the sourcemodel, and this new layer should do some transformations on the real one.
My problem is, that in the top layer, say "layer model" the data( const QModelIndex & index, int role ) member function never called, however i would like to alter the displaying methods by the role parameter.
Here is a sample code, which demonstrates, that the original model's data(index,role) is always called, whilst the layer model's data(index,role) never. Why? How can the QTableView object "skip" the top layer's data(index,role) ?
#include <QtGui/QApplication>
#include <QtGui>
#include <QStandardItemModel>
class MyModel : public QStandardItemModel
{
public:
MyModel(const int r, const int c, QObject* parent = 0) : QStandardItemModel(r,c,parent) {}
QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const {
qDebug() << "mymodel data";
return this->itemFromIndex(index)->data(role);
}
};
class MyProxyModel : public QAbstractProxyModel
{
public:
MyProxyModel(QObject* parent = 0) : QAbstractProxyModel(parent) {}
QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
return this->sourceModel()->index(row,column,parent);
}
QModelIndex parent ( const QModelIndex & index ) const {
return this->sourceModel()->parent(index);
}
QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const
{
return sourceIndex;
}
QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const
{
return proxyIndex;
}
QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const {
qDebug() << "myproxymodel data";
return this->sourceModel()->data(index,role);
}
int rowCount ( const QModelIndex & parent = QModelIndex() ) const {
return this->sourceModel()->rowCount(parent);
}
int columnCount ( const QModelIndex & parent = QModelIndex() ) const {
return this->sourceModel()->columnCount(parent);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
MyModel model(8, 2);
MyProxyModel mymodel;
mymodel.setSourceModel(&model);
QTableView tableView;
tableView.setModel(&mymodel);
tableView.horizontalHeader()->setStretchLastSection(true);
for (int row = 0; row < 8; ++row) {
for (int column = 0; column < 2; ++column) {
QModelIndex index = model.index(row, column, QModelIndex());
model.setData(index, QVariant((row+1) * (column+1)));
}
}
tableView.show();
return app.exec();
}
Because QTableView uses the model index to retrieve the data, probably something like this.
QModelIndex index = model->index(row, column, parentIndex);
index.data(Qt::DisplayRole);
And you are returning the model index of the source model instead of an index to your proxy model:
QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
return this->sourceModel()->index(row,column,parent);
}
Try to convert the model index into an index to your proxy model
QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
return this->createIndex(row,column,row);
}
Don't forget to rewrite the map to source and map from source functions.
Solution
class MyTableProxyModel : public QAbstractProxyModel
{
Q_OBJECT
public:
MyTableProxyModel (QObject* parent = 0)
: QAbstractProxyModel(parent) {
}
QModelIndex index(int row, int column, const QModelIndex& parent=QModelIndex()) const {
return createIndex(row,column,row);
}
QModelIndex parent(const QModelIndex &index) const {
//Works only for non-tree models
return QModelIndex();
}
QModelIndex mapFromSource(const QModelIndex &source) const {
return index(source.row(), source.column(), source.parent());
}
QModelIndex mapToSource(const QModelIndex &proxy) const {
return (sourceModel()&&proxy.isValid())
? sourceModel()->index(proxy.row(), proxy.column(), proxy.parent())
: QModelIndex();
}
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const {
qDebug() << "myproxymodel data";
return mapToSource(index).data(role);
}
int rowCount ( const QModelIndex & parent = QModelIndex() ) const {
return sourceModel() ? sourceModel()->rowCount(parent) : 0;
}
int columnCount ( const QModelIndex & parent = QModelIndex() ) const {
return sourceModel() ? sourceModel()->columnCount(parent) : 0;
}
};

Resources