I've two list of value. the first is list of integer and second is a list of strings. i want to display these different lists based on the last content of lineEdit. For Example when the user enter A => QCompleter show me list of strings. If the user enter A1 => QCompleter show me list of integer because the last character of input is 1.
I try like this :
void QtGuiApplication1::maFonction(QString text)
{
QString lastCaractor = text.mid(text.length() - 1, 1);
if (is_number(lastCaractor.toStdString()) == true)
{
QStringList list = { "1","2","7" };
StringCompleter->setModel(new QStringListModel(list, StringCompleter));
StringCompleter->setCompletionPrefix(lastCaractor);
}
else
{
QStringList list = { "AAAA","AA","BB","AC" };
StringCompleter->setModel(new QStringListModel(list, StringCompleter));
StringCompleter->setCompletionPrefix(lastCaractor);
}
}
bool QtGuiApplication1::is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
If The last character is number => QCompleter show me list = { "1","2","7" };
Else => list = { "AAAA","AA","BB","AC" };
Subclass your QLineEdit with 2 customs QCompleter
ExtendedLineEdit.h
class ExtendedLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit ExtendedLineEdit(QWidget *parent = nullptr);
void setWordCompleter(QCompleter* c);
void setNumberCompleter(QCompleter* c);
protected:
void keyPressEvent(QKeyEvent *e);
private slots:
void insertCompletionWord(const QString& txtComp);
void insertCompletionNumber(const QString& txtComp);
private:
QCompleter* m_completerWord;
QCompleter* m_completerNumber;
void showCustomCompleter(QCompleter* completer);
};
ExtendedLineEdit.cpp
ExtendedLineEdit::ExtendedLineEdit(QWidget *parent) :
QLineEdit(parent)
{
}
void ExtendedLineEdit::setWordCompleter(QCompleter *c)
{
m_completerWord = c;
m_completerWord->setWidget(this);
connect(m_completerWord, SIGNAL(activated(QString)),
this, SLOT(insertCompletionWord(QString)));
}
void ExtendedLineEdit::setNumberCompleter(QCompleter *c)
{
m_completerNumber = c;
m_completerNumber->setWidget(this);
connect(m_completerNumber, SIGNAL(activated(QString)),
this, SLOT(insertCompletionNumber(QString)));
}
void ExtendedLineEdit::keyPressEvent(QKeyEvent *e)
{
QLineEdit::keyPressEvent(e);
if (!m_completerWord || !m_completerNumber)
return;
QString lastCaractor = text().mid(text().length() - 1, 1);
bool okConvertion = false;
lastCaractor.toInt(&okConvertion);
if (okConvertion)
{
//show number completer
m_completerWord->popup()->hide();
m_completerNumber->setCompletionPrefix(lastCaractor);
showCustomCompleter(m_completerNumber);
}
else
{
//show word completer
m_completerNumber->popup()->hide();
m_completerWord->setCompletionPrefix(this->text());
showCustomCompleter(m_completerWord);
}
}
void ExtendedLineEdit::insertCompletionWord(const QString &txtComp)
{
setText(txtComp);
}
void ExtendedLineEdit::insertCompletionNumber(const QString &txtComp)
{
setText(text().mid(0, text().length() - 1) + txtComp);
}
void ExtendedLineEdit::showCustomCompleter(QCompleter *completer)
{
if (completer->completionPrefix().length() < 1)
{
completer->popup()->hide();
return;
}
QRect cr = cursorRect();
cr.setWidth(completer->popup()->sizeHintForColumn(0) + completer->popup()->verticalScrollBar()->sizeHint().width());
completer->complete(cr);
}
And using
QStringList list = { "AAAA","AA","BB","AC" };
m_wordCompleter = new QCompleter(list, this);
QStringList numbers = { "1","2","7", "15" };
m_numberCompleter = new QCompleter(numbers, this);
ui->lineEdit->setWordCompleter(m_wordCompleter);
ui->lineEdit->setNumberCompleter(m_numberCompleter);
Related
I am currently working on a ticker client, that polls data from a web api, hands it to a ListModel, which is then used to display the data in a qml ListView.
class TickerClient : public QObject
{
Q_OBJECT
public:
explicit TickerClient(QObject *parent = nullptr);
QList<QVariantMap> items() const;
protected:
void registerErrorHandlers(QNetworkReply *reply);
signals:
void statusChanged(QNetworkAccessManager::NetworkAccessibility acc);
void dataChanged();
void preItemRefresh();
void postItemRefresh();
public slots:
void fetch(int start = 0, int limit = 100);
protected slots:
void onReceive(QNetworkReply *reply);
protected:
QSharedPointer<QNetworkAccessManager> mNetMan;
QList<QVariantMap> mItems;
};
class TickerModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(TickerClient *client READ client WRITE setClient)
public:
explicit TickerModel(QObject *parent = nullptr);
enum {
IdRole = Qt::UserRole + 1,
NameRole,
SymbolRole,
...
};
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;
TickerClient *client() const;
void setClient(TickerClient *client);
private:
TickerClient *mClient;
};
The ticker client does not only fetch, but also handle the fetched data and expose it to the surrounding ListModel as a list of QVariantMaps.
void TickerClient::onReceive(QNetworkReply *reply)
{
if (!reply)
return;
if(reply->error()) {
qCritical() << "Error: "
<< reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString();
return;
}
// Read all data as json document.
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
if(!jsonDoc.isArray()) {
qCritical() << "Error: Expected array";
return;
}
emit preItemRefresh();
mItems.clear();
QJsonArray currencies = jsonDoc.array();
for(int i = 0; i < currencies.count(); i++) {
QJsonObject currency = currencies[i].toObject();
mItems.append(currency.toVariantMap());
}
emit postItemRefresh();
reply->deleteLater();
}
Both, the TickerClient and the TickerModel are exposed to qml:
qmlRegisterType<TickerModel>("Ticker", 1, 0, "TickerModel");
qmlRegisterUncreatableType<TickerClient>("Ticker", 1, 0, "TickerClient",
QStringLiteral("MarketCapProvider should not be created in QML"));
TickerClient tickerClient;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("tickerClient", &tickerClient);
The exposed client is then handed to the model and refreshed every 5 seconds:
ListView {
id: page
Timer {
interval: 5000
running: true
repeat: true
triggeredOnStart: true
onTriggered: {
var pos = scrollBar.position
tickerClient.fetch()
scrollBar.position = pos
}
}
ScrollBar.vertical: ScrollBar {
id: scrollBar
}
model: TickerModel {
client: tickerClient
}
delegate: RowLayout {
width: parent.width
spacing: 10
Label {
text: "#" + model.rank
padding: 5
}
Label {
text: model.name
padding: 5
}
}
}
However, the fetching of the data doesn't seem to work well as the wrong data is returned. So if I ask for model.name, I might end of with model.rank. The following code is used to fetch the entry for a given pair of index and role.
QVariant TickerModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !mClient)
return QVariant();
auto it = roleNames().find(role);
if(it != roleNames().end())
return mClient->items().at(index.row())[it.value()];
else
qDebug() << "Didn't match a role";
return QVariant();
}
QHash<int, QByteArray> TickerModel::roleNames() const
{
static const QHash<int, QByteArray> names{
{ IdRole, "id" },
{ NameRole, "name" },
...
};
return names;
}
TickerClient *TickerModel::client() const
{
return mClient;
}
void TickerModel::setClient(TickerClient *client)
{
beginResetModel();
if(mClient) {
mClient->disconnect(this);
}
mClient = client;
if(mClient) {
connect(mClient, &TickerClient::preItemRefresh, this, [=]() {
beginResetModel();
});
connect(mClient, &TickerClient::postItemRefresh, this, [=]() {
endResetModel();
});
}
endResetModel();
}
What, am I doing wrong and how can I extend this solution for more complex Json objects?
You are clearing mItems and then appending new items to it in onReceive method. You are not showing if model is listening to postItemRefresh signal. Anyway, you should call beginResetModel and endResetModel.
E.g. add following slot to your TickerModel and connect it to postItemRefresh signal to get your model synched witht the new data:
void TickerModel::reset()
{
beginResetModel();
endResetModel();
}
Edit:
You should add prints or go through with the debugger with what parameters your model data() is called when things get screwed. Also, make sure beginResetModel and endResetModel are called when items are updated.
Is your rowCount returning mClient->items().count() ?
To handle more complex json objects, instead of:
QJsonArray currencies = jsonDoc.array();
for(int i = 0; i < currencies.count(); i++) {
QJsonObject currency = currencies[i].toObject();
mItems.append(currency.toVariantMap());
}
You don't handle QVariantMap but parse more complex json objects to your own class which provides getters for data:
QJsonArray currencies = jsonDoc.array();
for(int i = 0; i < currencies.count(); i++) {
QJsonObject currency = currencies[i].toObject();
ComplexCurrency c;
c.read(currency);
mItems.append(c);
}
Your class:
class ComplexCurrency
{
public:
int id() const;
QString name() const;
bool enabled() const;
QList<QVariantMap> items();
void read(const QJsonObject &json);
private:
int m_id;
QString m_name;
bool m_enabled;
QList<QVariantMap> m_items;
...
};
void ComplexCurrency::read(const QJsonObject &json)
{
m_id = json["id"].toInt();
m_name = json["name"].toString();
m_enabled = json["enabled"].toBool();
// parse items array here
}
And then in model data():
QVariant TickerModel::data(const QModelIndex &index, int role) const
{
if ((index.row() < 0 || index.row() >= mClient->items().count()) || !mClient)
return QVariant();
const ComplexCurrency &c = mClient->items().at(index.row());
if (role == NameRole)
return c.name();
else if (role == SomethingRole)
return c.something();
return QVariant();
}
I'm new to Qt, so I'm not sure what to use.
I thought I would have a QStringList to give to my QStringListModel and display it in a ListView.
Now, however, I need to divide QStringList in the values of 2 types. So, I need to have string + some typeId, not just one string, but QStringList is for one-dimensional list only.
Anyone could give an advice on what is the best way to try to implement this?
The solution is to use QAbstractListModel subclass as a Qt Quick model. An example of base class for a models (I use it for convenience):
// abstractobjectlistmodel.h
#pragma once
#include <QtCore>
struct AbstractObjectListModel
: public QAbstractListModel
{
explicit AbstractObjectListModel(QObject * const parent = Q_NULLPTR)
: QAbstractListModel{parent}
{ ; }
int rowCount(QModelIndex const & parent = {}) const Q_DECL_OVERRIDE Q_DECL_FINAL
{
Q_UNUSED(parent);
return items.count();
}
QVariant data(QModelIndex const & index, const int role = Qt::DisplayRole) const Q_DECL_OVERRIDE Q_DECL_FINAL
{
if (!index.isValid()) {
return {};
}
switch (role) {
case Qt::UserRole : {
return QVariant::fromValue(items[index.row()].data());
}
default : {
return {};
}
}
}
QHash< int, QByteArray > roleNames() const Q_DECL_OVERRIDE Q_DECL_FINAL
{
auto roleNames = QAbstractListModel::roleNames();
roleNames.insert(Qt::UserRole, "modelData");
return roleNames;
}
Q_INVOKABLE
virtual
QObject * get(int row) const
{
if (row < 0) {
return {};
}
if (row >= rowCount()) {
return {};
}
return items[row];
}
void remove(int row, int count = 1)
{
Q_ASSERT(count > 0);
Q_ASSERT(row >= 0);
Q_ASSERT(row + count <= rowCount());
beginRemoveRows({}, row, row + count - 1);
while (0 < count) {
items.takeAt(row)->deleteLater();
--count;
}
endRemoveRows();
}
void clear()
{
if (!items.isEmpty()) {
remove(0, rowCount());
}
}
protected :
~AbstractObjectListModel() Q_DECL_OVERRIDE Q_DECL_EQ_DEFAULT; // derived classes should not meant to be manipulated polymorphically
QList< QPointer< QObject > > items;
void insert(int row, QObject * const item)
{
item->setParent(this);
beginInsertRows({}, row, row);
items.insert(row, item);
endInsertRows();
}
void append(QObject * const item)
{
insert(rowCount(), item);
}
};
But one need to override get to access items' Q_PROPERTY properties (in addition to dynamic ones):
// type of element
class Project
: public QObject
{
Q_OBJECT
Q_PROPERTY(QString name MEMBER name NOTIFY nameChanged)
Q_PROPERTY(QString path MEMBER path NOTIFY pathChanged)
public :
Project(QString name, QString path,
QObject * const parent = Q_NULLPTR)
: QObject{parent}
, name{name}
, path{path}
{ ; }
Q_SIGNALS :
void nameChanged(QString name);
void pathChanged(QString path);
private :
QString name;
QString path;
};
// custom model
class ProjectsListModel
: public AbstractObjectListModel
{
Q_OBJECT
public :
explicit ProjectsListModel(QObject * const parent = Q_NULLPTR)
: AbstractObjectListModel{parent}
{ ; }
void appendProject(QString name, QString path)
{
AbstractObjectListModel::append(::new Project{name, path});
}
Q_INVOKABLE
Project *
get(int row) const Q_DECL_OVERRIDE
{
return qobject_cast< Project * >(AbstractObjectListModel::get(row));
}
};
Before use one need to register concrete model with qmlRegisterType< ProjectsListModel >();. Properties of Project class are avaliable in delegate and highlight by means of members of modelData.
Another example:
struct TimeZoneModel Q_DECL_FINAL
: public QAbstractListModel
{
Q_OBJECT
public :
explicit TimeZoneModel(QObject * const parent = Q_NULLPTR)
: QAbstractListModel{parent}
{ ; }
int rowCount(QModelIndex const & parent = {}) const Q_DECL_OVERRIDE
{
Q_UNUSED(parent);
return timeZoneIds.count();
}
QVariant data(QModelIndex const & index, const int role = Qt::DisplayRole) const Q_DECL_OVERRIDE
{
if (!index.isValid() || (role > Qt::UserRole + 4)) {
return {};
}
QTimeZone timeZone{timeZoneIds[index.row()]};
if (!timeZone.isValid()) {
return {};
}
return roleData(timeZone, role);
}
QHash< int, QByteArray > roleNames() const Q_DECL_OVERRIDE
{
auto roleNames = QAbstractListModel::roleNames();
int i = Qt::UserRole;
for (const auto role : {"modelData", "id", "comment", "name", "country"}) {
roleNames.insert(i++, role);
}
return roleNames;
}
Q_INVOKABLE
QByteArray get(int row) const
{
if (row < 0) {
return {};
}
if (row >= rowCount()) {
return {};
}
return timeZoneIds[row];
}
private :
QVariant roleData(QTimeZone const & timeZone, int role = Qt::UserRole) const
{
switch (role) {
case Qt::UserRole : {
QVariantMap modelData;
const auto names = roleNames();
while (++role < Qt::UserRole + 5) {
modelData.insert(QString::fromUtf8(names[role]), roleData(timeZone, role));
}
return modelData;
}
case Qt::UserRole + 1: {
return QString::fromUtf8(timeZone.id());
}
case Qt::UserRole + 2 : {
return timeZone.comment();
}
case Qt::UserRole + 3 : {
return timeZone.displayName(QTimeZone::StandardTime);
}
case Qt::UserRole + 4 : {
return QLocale::countryToString(timeZone.country());
}
default : {
return {};
}
}
}
const QByteArrayList timeZoneIds = QTimeZone::availableTimeZoneIds();
};
In addition to access via modelData's fields (modelData.id, modelData.comment etc) all the symbols are accessible directly (i.e. id, comment etc) in delegate and highlight contexts of the ListView.
Model TimeZoneModel is const and can be injected into global scope directly without any performance drawbacks:
QQmlApplicationEngine engine;
TimeZoneModel timeZoneModel;
Q_SET_OBJECT_NAME(timeZoneModel);
qmlRegisterType< TimeZoneModel >();
const auto rootContext = engine.rootContext();
rootContext->setContextProperty(timeZoneModel.objectName(), &timeZoneModel);
If you need dictionary that contains QString and any other type I suggest you to use
QMap<QString, YourType> myMap;
Here you have some example of usage:
QMap<int, QString> myMap;
myMap.insert(1,"A");
myMap.insert(2,"B");
myMap[3] = "C";
foreach(int i, myMap.keys()) qDebug() << myMap[i];
I'm using a Qt property browser widget. I'm creating all the values of the property grid automatically using the moc but I didn't find how to make the values entered in the property grid be set automatically in my class instance? is there any sginal where I can know that?
I creating the property grid like this:
QWidget *w = new QWidget();
Foo *o = new Foo();
QtBoolPropertyManager *boolManager = new QtBoolPropertyManager(w);
QtIntPropertyManager *intManager = new QtIntPropertyManager(w);
QtStringPropertyManager *stringManager = new QtStringPropertyManager(w);
QtGroupPropertyManager *groupManager = new QtGroupPropertyManager(w);
const QMetaObject *meta = o->metaObject();
const QString className = meta->className();
QMap<QString, PropManager*> dic;
QtProperty *root = groupManager->addProperty(className);
for(int i = meta->propertyOffset(); i < meta->propertyCount(); ++i)
{
QMetaProperty metaProp = meta->property(i);
const QString name = metaProp.name();
QVariant value = metaProp.read(o);
QVariant::Type t = metaProp.type();
QtProperty *prop = NULL;
PropManager *propMan = NULL;
switch(t)
{
case QVariant::Int:
{
prop = intManager->addProperty(name);
root->addSubProperty(prop);
intManager->setValue(prop, value.toInt());
propMan = new PropManager(prop, intManager, t);
break;
}
case QVariant::String:
{
prop = stringManager->addProperty(name);
root->addSubProperty(prop);
stringManager->setValue(prop, value.toString());
propMan = new PropManager(prop, stringManager, t);
break;
}
default:
throw std::invalid_argument("unknow type");
}
if(prop != NULL)
dic.insert(name, propMan);
}
QtCheckBoxFactory *checkBoxFactory = new QtCheckBoxFactory(w);
QtSpinBoxFactory *spinBoxFactory = new QtSpinBoxFactory(w);
QtLineEditFactory *lineEditFactory = new QtLineEditFactory(w);
QtAbstractPropertyBrowser *editor = new QtTreePropertyBrowser();
editor->setFactoryForManager(boolManager, checkBoxFactory);
editor->setFactoryForManager(intManager, spinBoxFactory);
editor->setFactoryForManager(stringManager, lineEditFactory);
editor->addProperty(root);
QGridLayout *layout = new QGridLayout(w);
layout->addWidget(editor, 1, 0);
w->show();
Foo.h is defined like this:
class Foo : public QObject
{
Q_OBJECT
public:
explicit Foo() { }
~Foo() { }
Q_PROPERTY(QString name READ getA WRITE setA)
Q_PROPERTY(int age READ getNum WRITE setNum)
QString name;
int age;
QString getA() const { return name; }
int getNum() const { return age; }
void setA(QString a) { this->name = a; }
void setNum(int n) { this->age = n; }
};
Partial solution:
I found propertyChanged() from QtAbstractPropertyManager but this does raise every time a single character is edited. I'd like to have an slot where this is fired when the user is done with the editing.
In this program I have a class inheriting of QGraphicsObject. This object (mario) go forward, go back and jump. For this jobs I changed mario's coordinate. But I have a problem. I want it to stop when collide with bricks. While I don't use of QPropertyAnimation how to stop this item.
extern mario* _mario=new mario;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
size_of_plane_y=600;
size_of_plane_x=2000;
view=new QGraphicsView;
scene=new QGraphicsScene;
rec=new QGraphicsRectItem;
setCentralWidget(view);
view->setScene(scene);
scene->setSceneRect(0,0,size_of_plane_x,size_of_plane_y);
scene->addRect(scene->sceneRect());
x_scene=0;
y_scene=0;
int tmpb=8*30+120+10*30+120;
int tmpb2=10*30+120+8*30+120;
int firstb=100;
int firstb2=100+8*30+120;
for(int i=0;i<3;i++)
{
_brick=new brick(0,firstb,size_of_plane_y-180);
scene->addItem(_brick);
_brick2=new brick2(0,firstb2,size_of_plane_y-180);
scene->addItem(_brick2);
firstb+=tmpb;
firstb2+=tmpb2;
}
timer1=new QTimer(this);
timer1->setInterval(500);
connect(timer1,SIGNAL(timeout()),this,SLOT(up()));
};
void MainWindow::keyPressEvent(QKeyEvent *k)
{
switch (k->key())
{
case Qt::Key_J:
{
forward();
break;
}
case Qt::Key_Z:
{
timer1->start();
break;
}
case Qt::Key_F:
{
back();
break;
}
default:
break;
}
}
void MainWindow::forward()
{
if(ismovepossible(_mario->pos().x()+50,_mario->pos().y())==true)
{
_mario->setX(_mario->pos().x()+50);
scene->setSceneRect(x_scene+40,y_scene,size_of_plane_x,size_of_plane_y);
x_scene+=40;
}
}
void MainWindow::up()
{
static bool flag=0;
if(!flag)
{
if(ismovepossible(_mario->pos().x(),_mario->pos().y()-90)==true)
{
_mario->setY(_mario->pos().y()-90);
flag=1;
}
}
else
{
if(ismovepossible(_mario->pos().x(),_mario->pos().y()-90)==true)
{
_mario->setY(_mario->pos().y()+90);
timer1->stop();
flag=0;
}
}
}
void MainWindow::back()
{
if(ismovepossible(_mario->pos().x()-50,_mario->pos().y())==true)
{
_mario->setX(_mario->pos().x()-50);
scene->setSceneRect(x_scene-50,y_scene,size_of_plane_x,size_of_plane_y);
x_scene-=50;
}
}
////////////////edit
void MainWindow::projectile()//when user press Z and J simultaneity
{
static bool flag=0;
if(!flag)
{
if(ismovepossible(_mario->pos().x()+50,_mario->pos().y()-90)==true)
{
_mario->setY(_mario->pos().y()-90);
_mario->setX(_mario->pos().x()+50);
scene->setSceneRect(x_scene+50,y_scene,size_of_plane_x,size_of_plane_y);
x_scene-=50;
text->setPos(x_scene+200,10);
flag=1;
}
}
else
{
if(ismovepossible(_mario->pos().x(),_mario->pos().y()+90)==true)
{
_mario->setY(_mario->pos().y()+90);
timer2->stop();
flag=0;
}
}
}
/////////////////////add function
bool MainWindow::ismovepossible(int x, int y)
{
int dis_b=10*30+120+120;
int dis_b2=4*30+120+8*30+120;//8*30+120+120;
int firstb=100;
int endb=/*firstb*/100+8*30;
int firstb2=/*endb*/100+8*30+120;
int endb2=/*firstb2*/100+8*30+120+4*30;
int firstb3=/*endb2*/100+8*30+120+4*30+2*30;
int endb3=/*firstb3*/100+8*30+120+4*30+2*30+4*30;
int dis_b3=120+8*30+120+4*30+2*30;
if(y>size_of_plane_y-60 && y<=size_of_plane_y)
{
return false;
}
while(endb<size_of_plane_x)
{
if((y>size_of_plane_y-180 && y<size_of_plane_y-180+30)&&(x>firstb && x<endb))
{
return false;
}
else
{
firstb+=dis_b;
endb+=firstb;
}
}
while(endb2<size_of_plane_x)
{
if((y>size_of_plane_y-180 && y<size_of_plane_y-180+30)&&(x>firstb2 && x<endb2))
{
return false;
}
else
{
firstb2+=dis_b2;
endb2+=firstb2;
}
}
while(endb3<size_of_plane_x)
{
if((y>size_of_plane_y-180 && y<size_of_plane_y-180+30)&&(x>firstb3 && x<endb3))
{
return false;
}
else
{
firstb3+=dis_b3;
endb3+=firstb3;
}
}
return true;
}
class brick : public QGraphicsObject
{
Q_OBJECT
public:
explicit brick(QGraphicsItem *parent = 0,int x=100,int y=600-180);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QRectF boundingRect()const;
public slots:
void collision();
private:
int size_of_plane_y;
int start_x;
int start_y;
int size_brick;
};
brick::brick(QGraphicsItem *parent, int x, int y) : QGraphicsObject(parent)
{
size_of_plane_y=600;
start_x=x;
start_y=y;
size_brick=30;
connect(_mario,SIGNAL(xChanged()),this,SLOT(collision()));
connect(_mario,SIGNAL(yChanged()),this,SLOT(collision()));
}
void brick::collision()
{
if(this->collidesWithItem(_mario))
{
qDebug()<<"collision";
//what write here for stopping super mario?
}
}
I think you should modify your algorithm. The main problem here is you move mario before checking if there is a brick.
You move mario, you emit the signal xChanged() or yChanged(), check if you're on a brick or not ? Then you have to go back to the previous location ?
Since you know where you put your bricks, you can do something more like this:
void MainWindow::forward()
{
if(moveIsPossible(xDest, yDest))
{
_mario->setX(...);
[...]
}
In the moveIsPossible(xDest, yDest), you put the tuple (X,Y) of the future location, it returns true if you have no brick, no wall, no whatever you want, false otherwise.
Seeing your code, I just want to draw your attention on many thing:
Try to use const variable to put any configuration variable such as board size, brick size, etc ...
Try to create specific classes for specific function. forward(), back(), etc.. have not to be implemented in the mainwindow class.
My main goal here is to redefine operator< so that I can sort my columns numerically instead of alphabetically. The code I have so far:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
//bunch of includes...
#include <QTreeWidgetItem>
#include <QTreeWidget>
extern QSqlDatabase db;
extern QString prefix;
extern QString site;
extern QString dbname;
extern QString user;
namespace Ui {
class mainwindow;
}
class clientinfo;
class mainwindow : public QMainWindow
{
Q_OBJECT
public:
explicit mainwindow(QWidget *parent = 0);
void rPopulate();
void rbookingdisplay();
QPushButton *button;
~mainwindow();
public slots:
//slots...
private:
Ui::mainwindow *ui;
void closeEvent(QCloseEvent *close);
};
class TreeWidgetItem : public QTreeWidgetItem
{
public:
TreeWidgetItem(QTreeWidget *tree) : QTreeWidgetItem(tree) {}
TreeWidgetItem(QTreeWidget * parent, const QStringList & strings)
: QTreeWidgetItem (parent,strings) {}
bool operator< (const QTreeWidgetItem &other) const
{
int sortCol = treeWidget()->sortColumn();
int myNumber = text(sortCol).toInt();
int otherNumber = other.text(sortCol).toInt();
return myNumber < otherNumber;
}
};
#endif // MAINWINDOW_H
and in mainwindow.cpp I have ui->_display->setSortingEnabled(true);
somehow it doesn't use the redefined operator< (the redefined function is never called, it uses the predefined one). Please help.
ui->_abooking->clear();
ui->_abooking->setSortingEnabled(false);
QSqlQuery query;
query.prepare(" SELECT shownumdays, shownumpastdays FROM setting_"+ user +
" WHERE id = 1; ");
query.exec();
query.first();
int days = query.record().value("shownumdays").toInt();
int pastdays = query.record().value("shownumpastdays").toInt();
query.prepare(" SELECT `"+Columns.join("`, `")+
"`,`Currency`, `# of Room` "
" FROM abooking "
" WHERE (DATE_ADD(`Arrival Date`,INTERVAL `Nights` - 1 DAY) >= (CURRENT_DATE - "+ QString::number(pastdays) +") AND "
" (TO_DAYS(`Arrival Date`) + `Nights` - 1 - TO_DAYS(NOW())) <= "+ QString::number(days) +") ");
query.exec();
QString confirm_num = "";
bool flip = true;
while (query.next()){
if (!(confirm_num == query.record().value("Confirmation #").toString())) {
flip = (!flip);
}
QTreeWidgetItem *item = new QTreeWidgetItem();
for (int i = 0; i < Columns.length(); i++) {
if (Columns.at(i) == "Arrival Date") {
item->setText(i, query.record().value(Columns.at(i)).toDate().toString("dd-MM-yyyy"));
} else if ((Columns.at(i) == "Amount Due" || Columns.at(i) == "Paid") && (confirm_num == query.record().value("Confirmation #").toString())) {
item->setText(i, "");
} else if ((Columns.at(i) == "Arrival Time")){
item->setText(i, " " + query.record().value(Columns.at(i)).toTime().toString("hh:mm"));
} else {
item->setText(i, query.record().value(Columns.at(i)).toString());
}
if (Columns.at(i) == "Amount Due" && (!(item->text(i) == ""))) {
item->setTextColor(i, Qt::darkRed);
item->setText(i, item->text(i) + " " + query.record().value("Currency").toString());
} else if (Columns.at(i) == "Room") {
item->setText(i, query.record().value("# of Room").toString() + " " + item->text(i));
}
if (flip) {
item->setBackground(i , Qt::gray);
}
}
if ((!(confirm_num == query.record().value("Confirmation #").toString()))) {
confirm_num = query.record().value("Confirmation #").toString();
ui->_abooking->addTopLevelItem(item);
} else {
ui->_abooking->topLevelItem(ui->_abooking->topLevelItemCount() - 1)->addChild(item);
ui->_abooking->topLevelItem(ui->_abooking->topLevelItemCount() - 1)->setExpanded(true);
}
}
ui->_abooking->setSortingEnabled(true);
You're reimplementing QTreeWidgetItem with a custom class, but then you don't use it when populating the QTreeWidget.
Instead of
QTreeWidgetItem *item = new QTreeWidgetItem();
this should be:
TreeWidgetItem *item = new TreeWidgetItem();
Other than that, your code for operator < looks fine. I'm using the same and it works for me.
bool operator< (const QTreeWidgetItem &other) const
{
int sortCol = treeWidget()->sortColumn();
int myNumber = text(sortCol).toInt();
int otherNumber = other.text(sortCol).toInt();
return myNumber < otherNumber;
}
With reference to your code here above:
I think your problem is that the sort column on the widget remains constant for all QTreeWidgetItems. You have to sort on the value of the item (not the column of the widget):
Therefore the code should look like this:
bool operator< (const QTreeWidgetItem &other) const
{
Q_ASSERT( other.treeWidget() == treeWidget() );
const int col = treeWidget()->sortColumn();
const int role = Qt::DisplayRole;
return( data( col, role ).toInt() < other.data( col, role ).toInt() );
}
The cells are sorted according to the type of data you store in them. You don't need to redefine an operator for that, because QVariant will take care of the comparison for you.
For example, you should use:
item−>setData(0, `Qt::EditRole`, 10);
instead of:
item->setText(0, "10");