QML Combobox reports ReferenceError: modelData is not defined - combobox

I have following simple QML Combobox:
import QtQuick 2.0
import QtQuick.Controls 1.4
import si.mikroelektronika 1.0
Item
{
id: ueStaffSelector
width: 256
height: 96
ComboBox
{
model: uePeopleModel
editable: false
anchors.fill: parent
} // ComboBox
} // Item
As you can see, I assign uePeopleModel to it, which is working ok already in app. Once the app is executed, I get following QML runtime errors:
file:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ComboBox.qml:562:
ReferenceError: modelData is not defined
file:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ComboBox.qml:562:
ReferenceError: modelData is not defined
file:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ComboBox.qml:562:
ReferenceError: modelData is not defined
file:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ComboBox.qml:562:
ReferenceError: modelData is not defined
file:///opt/QtOpenSource55/5.5/gcc_64/qml/QtQuick/Controls/ComboBox.qml:562:
ReferenceError: modelData is not defined
Whole application is constructed in main.cpp:
#include <QtQml>
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QTimer>
#include "database/uepeoplemodel.h"
#include "core/ueapplicationstatus.h"
#include "core/uedatabaseconnectionstatus.h"
#include "core/uebluetoothmanager.h"
#include "core/uebluetoothprinterconnectionstatus.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
UeApplicationStatus* ueApplicationStatus=new UeApplicationStatus(qApp);
UePeopleModel* uePeopleModel=new UePeopleModel(qApp);
UeBluetoothManager* ueBtManager=new UeBluetoothManager(qApp);
QObject::connect(uePeopleModel,
SIGNAL(ueSignalDatabaseConnectionChanged(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)),
ueApplicationStatus,
SLOT(ueSlotDatabaseConnectionChanged(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)));
QObject::connect(ueBtManager,
SIGNAL(ueSignalBtPrinterConnectionChanged(UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus)),
ueApplicationStatus,
SLOT(ueSlotBtPrinterConnectionChanged(UeBluetoothPrinterConnectionStatus::UeTypeBluetootPrinterConnectionStatus)));
engine.rootContext()->setContextProperty("uePeopleModel",
uePeopleModel);
engine.rootContext()->setContextProperty("ueApplicationStatus",
ueApplicationStatus);
engine.rootContext()->setContextProperty("ueBtManager",
ueBtManager);
engine.addImageProvider(QLatin1String("uePeopleModel"),
uePeopleModel);
qmlRegisterUncreatableType<UeDatabaseConnectionStatus>("si.mikroelektronika",
1,
0,
"UeTypeDatabaseConnectionStatus",
"Database Connection Status");
qmlRegisterUncreatableType<UeBluetoothPrinterConnectionStatus>("si.mikroelektronika",
1,
0,
"UeTypeBluetootPrinterConnectionStatus",
"Bluetooth Printer Connection Status");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
uePeopleModel->ueConnectToDatabase();
ueBtManager->ueStartPairing();
//ueApplicationStatus->ueUpdate(uePeopleModel->ueFetchUsers());
return app.exec();
}
Here is also UePeopleModel header, file UePeopleModel.h:
#ifndef UEPEOPLEMODEL_H
#define UEPEOPLEMODEL_H
#include <QImage>
#include <QVariant>
#include <QStringList>
#include <QHash>
#include <QByteArray>
#include <QSqlError>
#include <QSqlQueryModel>
#include <QSqlRecord>
#include <QModelIndex>
#include <QQuickImageProvider>
#include <QByteArray>
#include <QSqlRecord>
#include <QSqlQuery>
#include "../settings/uedefaults.h"
#include "../core/uedatabaseconnectionstatus.h"
#include "../core/uetypes.h"
#include "../core/ueapplicationstatus.h"
#include "../core/ueuserrecord.h"
class UePeopleModel : public QSqlQueryModel,
public QQuickImageProvider
{
Q_OBJECT
private:
QSqlDatabase m_ueDb;
private:
QSqlDatabase ueDatabase() const
{ return this->m_ueDb; }
void ueSetDatabase(const QSqlDatabase& database)
{ this->m_ueDb=database; }
QImage ueImage(const QString& id) const;
public:
UePeopleModel(QObject *parent=0);
~UePeopleModel();
QVariant data(const QModelIndex &index,
int role) const Q_DECL_OVERRIDE;
QImage requestImage(const QString &id,
QSize *size,
const QSize &requestedSize);
UeTypeRoles roleNames() const;
void ueConnectToDatabase();
UeTypeUsers* ueFetchUsers();
public:
static const int ueRoleName=Qt::UserRole+1;
static const int ueRoleImage=Qt::UserRole+2;
static const int ueRolePassword=Qt::UserRole+3;
signals:
void ueSignalDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus& newStatus);
};
#endif // UEPEOPLEMODEL_H
and its implementation, file UePeopleModel.cpp:
#include "uepeoplemodel.h"
UePeopleModel::UePeopleModel(QObject* parent)
: QSqlQueryModel(parent),
QQuickImageProvider(QQmlImageProviderBase::Image,
QQmlImageProviderBase::ForceAsynchronousImageLoading)
{
emit this->ueSignalDatabaseConnectionChanged(UeDatabaseConnectionStatus::NOT_CONNECTED);
} // default constructor
UePeopleModel::~UePeopleModel()
{
QString connName=this->ueDatabase().connectionName();
this->ueDatabase().close();
this->ueSetDatabase(QSqlDatabase());
this->ueDatabase().removeDatabase(connName);
emit this->ueSignalDatabaseConnectionChanged(UeDatabaseConnectionStatus::NOT_CONNECTED);
} // default destructor
QVariant UePeopleModel::data(const QModelIndex &index,
int role) const
{
switch(role)
{
case ueRoleImage:
{
return QString::number(index.row());
} break; // case
case ueRoleName:
{
return this->record(index.row()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_NAME).toString();
} break; // case
case ueRolePassword:
{
return this->record(index.row()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_APPPASSWORD).toString();
} break; // case
default:
{
return QSqlQueryModel::data(index,
role);
} break; // default
} // switch
return QVariant();
} // data
QImage UePeopleModel::ueImage(const QString &id) const
{
return QImage::fromData(this->record(id.toInt()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_IMAGE).toByteArray(),
"PNG").scaled(UeDefaults::UeGraphics::PEOPLE_IMAGE_WIDTH,
UeDefaults::UeGraphics::PEOPLE_IMAGE_HEIGHT,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
} // ueImage
QImage UePeopleModel::requestImage(const QString &id,
QSize *size,
const QSize &requestedSize)
{
Q_UNUSED(size)
Q_UNUSED(requestedSize);
// if(size)
// {
// *size=QSize(UeDefaults::UeGraphics::PEOPLE_IMAGE_WIDTH,
// UeDefaults::UeGraphics::PEOPLE_IMAGE_HEIGHT);
// } // if
//return this->ueImage(id);
return QImage::fromData(this->record(id.toInt()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_IMAGE).toByteArray(),
"PNG").scaled(UeDefaults::UeGraphics::PEOPLE_IMAGE_WIDTH,
UeDefaults::UeGraphics::PEOPLE_IMAGE_HEIGHT,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
} // requestImage
UeTypeRoles UePeopleModel::roleNames() const
{
UeTypeRoles roles;
const int iRoleName=UePeopleModel::ueRoleName;
const int iRoleImage=UePeopleModel::ueRoleImage;
const int iRolePassword=UePeopleModel::ueRolePassword;
roles.insert(iRoleName,
"ueRoleName");
roles.insert(iRoleImage,
"ueRoleImage");
roles.insert(iRolePassword,
"ueRolePassword");
return roles;
} // roleNames
void UePeopleModel::ueConnectToDatabase()
{
if(!QSqlDatabase::connectionNames().contains(UePosDatabase::UeDatabaseConnectionNames::DATABASE_CONNECTION_NAME_PEOPLE,
Qt::CaseInsensitive))
{
this->ueSetDatabase(QSqlDatabase::addDatabase(UePosDatabase::DATABASE_DRIVER,
UePosDatabase::UeDatabaseConnectionNames::DATABASE_CONNECTION_NAME_PEOPLE));
} // if
this->ueDatabase().setHostName(UePosDatabase::UeDatabaseConnectionParameters::DATABASE_HOSTNAME);
this->ueDatabase().setDatabaseName(UePosDatabase::UeDatabaseConnectionParameters::DATABASE_NAME);
this->ueDatabase().setUserName(UePosDatabase::UeDatabaseConnectionParameters::DATABASE_USERNAME);
this->ueDatabase().setPassword(UePosDatabase::UeDatabaseConnectionParameters::DATABASE_PASSWORD);
if(this->ueDatabase().open())
{
this->setQuery(UePosDatabase::UeSqlQueries::UeTablePeople::SQL_QUERY_GET_ALL_PEOPLE,
this->ueDatabase());
emit this->ueSignalDatabaseConnectionChanged(UeDatabaseConnectionStatus::CONNECTED);
}
else
{
emit this->ueSignalDatabaseConnectionChanged(UeDatabaseConnectionStatus::NOT_CONNECTED);
} // if
} // ueConnectToDatabase
UeTypeUsers* UePeopleModel::ueFetchUsers()
{
UeTypeUsers* users=new UeTypeUsers();
for(int iIndex=0; iIndex<this->record().count(); iIndex++)
{
users->append(new UeUserRecord(this,
this->record(iIndex).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_ID).toString(),
this->record(iIndex).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_NAME).toString(),
this->record(iIndex).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_APPPASSWORD).toString()));
} // for
return users;
} // ueFetchUsers
Why am I getting this error?

You need to tell the combobox which role to use. Try adding textRole: "ueRoleName" to your ComboBox.

Related

deleting Objects from MObjectArray in undo

As per below code first I am trying to append objects to MObjectArray , which I
want to delete in undo functionality.
Undo,Redo works fine .. But undo is deleting only first element of array/object
not all of them. Would like to know what's going wrong and what's best way to delete objects in Undo function.
.h
#include <maya/MPxCommand.h>
#include <maya/MArgList.h>
#include <maya/MObject.h>
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MDagPath.h>
#include <maya/MFnMesh.h>
#include <maya/MPointArray.h>
#include <maya/MIntArray.h>
#include <maya/MItSelectionList.h>
#include <maya/MItMeshEdge.h>
#include <maya/MDagModifier.h>
class deleteObj : public MPxCommand
{
public:
deleteObj() {};
virtual MStatus doIt(const MArgList& argList);
static void* creator();
virtual MStatus redoIt();
virtual MStatus undoIt();
MObjectArray objectTransforms;
virtual bool isUndoable() const { return true; }
};
.cpp
#include <maya\MFnPlugin.h>
#include "undoredo.h"
MStatus deleteObj::doIt(const MArgList & argList)
{
return redoIt();
}
MStatus deleteObj::redoIt()
{
for (int j = 0; j < 3; j++)
{
MString cmd("maya.cmds.polyCube()");
MGlobal::executePythonCommand(cmd);
MSelectionList selection;
MGlobal::getActiveSelectionList(selection);
MObject selObj;
selection.getDependNode(j, selObj);
objectTransforms.append(selObj);
}
return MS::kSuccess;
}
MStatus deleteObj::undoIt()
{
for (unsigned int i = 0; i <objectTransforms.length(); i++)
{
MGlobal::deleteNode(objectTransforms[i]);
}
return MS::kSuccess;
}
void * deleteObj::creator()
{
return new deleteObj;
}
MStatus initializePlugin(MObject obj)
{
MFnPlugin plugin(obj, "Atri Dave", "1.0", "Any");
MStatus status = plugin.registerCommand("deleteObj", deleteObj::creator);
CHECK_MSTATUS_AND_RETURN_IT(status);
return status;
}
MStatus uninitializePlugin(MObject obj)
{
MFnPlugin plugin(obj);
MStatus status = plugin.deregisterCommand("deleteObj");
CHECK_MSTATUS_AND_RETURN_IT(status);
return status;
}

How to return a new object created in node native code via asynchronous callback?

I am trying to create a node addon that does something like this:
// js
addon.makeObject((err, obj) => {
if (err) { /* handle error */ }
console.log('New object ID=%d', obj.getID());
:
});
The makeObject() is sort of an "asynchronous object factory" where a new instance is created inside the native C++ code in the background (using Nan::AsyncWorker) with necessary setups then returned back to NodeJS land via callback.
Here's a snippet of the native code:
// cpp
#include <nan.h>
#ifndef _WIN32
#include <unistd.h>
#define Sleep(x) usleep((x)*1000)
#endif
class MyClass : public Nan::ObjectWrap {
public:
class Worker : public Nan::AsyncWorker {
public:
friend class MyClass;
explicit Worker(Nan::Callback* callback) : Nan::AsyncWorker(callback) {}
private:
virtual void Execute() {/* some long running task */ Sleep(1000); }
virtual void HandleOKCallback() {
MyClass *mc = new MyClass();
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
mc->Wrap(obj); // ==> Assertion failed: (object->InternalFieldCount() > 0)
v8::Local<v8::Value> argv[] = { Nan::Undefined(), obj };
callback->Call(2, argv);
}
};
explicit MyClass() : _id(idCtr++) {}
~MyClass() {}
static void Init(v8::Local<v8::Object> exports) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("MyClass").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "getID", GetID);
constructor.Reset(tpl->GetFunction());
exports->Set(Nan::New("MyClass").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}
private:
int _id;
static Nan::Persistent<v8::Function> constructor;
static NAN_METHOD(New) {
if (info.IsConstructCall()) {
MyClass* mc = new MyClass();
mc->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
const int argc = 0;
v8::Local<v8::Value> argv[argc] = {};
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
}
}
static NAN_METHOD(GetID) {
MyClass *mc = ObjectWrap::Unwrap<MyClass>(info.Holder());
info.GetReturnValue().Set(Nan::New<v8::Integer>(mc->_id));
}
static int idCtr;
};
Nan::Persistent<v8::Function> MyClass::constructor;
int MyClass::idCtr = 0;
NAN_METHOD(MakeObject) {
// Check arguments here...
Nan::Callback *cb = new Nan::Callback(info[0].As<v8::Function>());
Nan::AsyncQueueWorker(new MyClass::Worker(cb)); // starts the worker
info.GetReturnValue().Set(Nan::Undefined());
}
void InitAll(v8::Local<v8::Object> exports) {
exports->Set( Nan::New("makeObject").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(MakeObject)->GetFunction());
MyClass::Init(exports);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)
Although this compiles, it fails at this line:
mc->Wrap(obj);
This is the error:
Assertion failed: (object->InternalFieldCount() > 0), function Wrap, file ../node_modules/nan/nan_object_wrap.h, line 55.
What is InternalFieldCount? I've been googling to find a way but with no luck so far...
I found an answer myself. The key was to use Nan::NewInstance on a constructor - the same way to handle JS code calling constructor as a normal function.
Here's the complete code that works:
// addon.cpp
#include <nan.h>
#ifndef _WIN32
#include <unistd.h>
#define Sleep(x) usleep((x)*1000)
#endif
class MyClass : public Nan::ObjectWrap {
public:
class Worker : public Nan::AsyncWorker {
public:
friend class MyClass;
explicit Worker(Nan::Callback* callback) : Nan::AsyncWorker(callback) {}
private:
virtual void Execute() {/* some long running task */ Sleep(1000); }
virtual void HandleOKCallback() {
///////////////////////////////////////////////////////////
// Create MyClass instance and pass it to JS via callback
const int argc = 0;
v8::Local<v8::Value> argv[argc] = {};
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
v8::Local<v8::Object> obj = Nan::NewInstance(cons, argc, argv).ToLocalChecked();
v8::Local<v8::Value> _argv[] = { Nan::Undefined(), obj };
callback->Call(2, _argv);
}
};
explicit MyClass() : _id(idCtr++) {}
~MyClass() {}
static NAN_MODULE_INIT(Init) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("MyClass").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "getID", GetID);
constructor.Reset(tpl->GetFunction());
target->Set(Nan::New("MyClass").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}
private:
int _id;
static Nan::Persistent<v8::Function> constructor;
static NAN_METHOD(New) {
if (info.IsConstructCall()) {
MyClass* mc = new MyClass();
mc->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
const int argc = 0;
v8::Local<v8::Value> argv[argc] = {};
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
info.GetReturnValue().Set(Nan::NewInstance(cons, argc, argv).ToLocalChecked());
}
}
static NAN_METHOD(GetID) {
MyClass *mc = ObjectWrap::Unwrap<MyClass>(info.Holder());
info.GetReturnValue().Set(Nan::New<v8::Integer>(mc->_id));
}
static int idCtr;
};
Nan::Persistent<v8::Function> MyClass::constructor;
int MyClass::idCtr = 0;
NAN_METHOD(MakeObject) {
// Check arguments here...
Nan::Callback *cb = new Nan::Callback(info[0].As<v8::Function>());
Nan::AsyncQueueWorker(new MyClass::Worker(cb)); // starts the worker
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_MODULE_INIT(InitAll) {
target->Set( Nan::New("makeObject").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(MakeObject)->GetFunction());
MyClass::Init(target);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, InitAll)

C - error LNK2005, _cmd already defined in complex

I got this error when compiling my code in visual studio and i need help please :
Error LNK2005: _cmd already defined in complex.obj
I have the following C Files :
### File name: Main.c ###
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "complex.h"
int main()
{
char command[30];
int i;
FOREVER
if (scanf("%s", command) == 1)
{
for (i = 0; cmd[i].func != NULL; i++)/*searcing the relevant function*/
{
if (strcmp(command, cmd[i].name) == 0)
break;
}
if (cmd[i].func == NULL)
printf("Command does not exist: %s\n", command);
else
(*(cmd[i]).func)();
}
}
and this:
### File name: Complex.h ###
#define FOREVER for(;;)
typedef struct complex
{
double real;
double imag;
}complex;
void read_comp(void);
void print_comp(void);
void add_comp(void);
void sub_comp(void);
void mult_comp_real(void);
void mult_comp_img(void);
void mult_comp_comp(void);
void abs_comp(void);
void halt(void);
void empty_string(void);
void stop(void);
struct STR{
char* name;
void(*func)(void);/*pointer to function*/
}cmd[] = {
{ "read_comp", read_comp },
{ "print_comp", print_comp },
{ "add_comp", add_comp },
{ "sub_comp", sub_comp },
{ "mult_comp_real", mult_comp_real },
{ "mult_comp_img", mult_comp_img },
{ "mult_comp_comp", mult_comp_comp },
{ "abs_comp", abs_comp },
{ "halt", halt },
{ "stop", stop }
};
and this:
### File name: Complex.c ###
#include "complex.h"
void stop(void)
{
exit(1);
}
void read_comp(void)
{
printf("read_comp\n");
}
void print_comp(void)
{
printf("print_comp\n");
}
void add_comp(void)
{
printf("add_comp\n");
}
void sub_comp(void)
{
printf("sub_comp\n");
}
void mult_comp_real(void)
{
printf("mult_comp_real\n");
}
void mult_comp_img(void)
{
printf("mult_comp_img\n");
}
void mult_comp_comp(void)
{
printf("mult_comp_comp\n");
}
void abs_comp(void)
{
printf("abs_comp\n");
}
void halt(void)
{
printf("halt\n");
}
void empty_string(void)
{
printf("Empty sting, Please try again\n");
}
We have here some functions that get some parameters as inputs through the command line. The functions implementation is not finished yet.
Please i need help with solving the error.
Since you define the struct cmd in complex.h it will be instantiated in both object files, which then leads to the linking error.
You should define the struct only in one of the two files and declare it extern it in your header file.
This goes into your header file:
struct STR{
char* name;
void(*func)(void);/*pointer to function*/
};
extern struct STR *cmd;
And this for example in complex.c:
struct STR cmd[] = {
{ "read_comp", read_comp },
{ "print_comp", print_comp },
{ "add_comp", add_comp },
{ "sub_comp", sub_comp },
{ "mult_comp_real", mult_comp_real },
{ "mult_comp_img", mult_comp_img },
{ "mult_comp_comp", mult_comp_comp },
{ "abs_comp", abs_comp },
{ "halt", halt },
{ "stop", stop }
};

Qt : Crash - was it the code or do I need to insert some other command?

When I run the following code, it throws an error
The inferior stopped because it received a signal from the operating system.
Signal name: SIGSEGV
Signal meaning: Segmentation fault
This is my code, I'm wondering where I went wrong?
I wanted to shown some random blocks on the window, but it got failed
mainwindow.cpp
#include "mainwindow.h"
#include <ctime>
#include <QPixmap>
#include <QPainter>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->setFixedSize(win_width,win_height);
this->setWindowIcon(QIcon(":/Image/icon.png"));
this->setWindowTitle("Qt POPONG");
startpage = new StartPage(this);
for (int i=0; i<80; i++){
block_[i] = new block(this);
}
gameRedy();
startpage->show();
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap bgImg;
bgImg.load(":/Image/bg.png");
painter.drawPixmap(0,0,win_width,win_height,bgImg);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if (event->key()==Qt::Key_S) {
gameStart();
startpage->close();
}
}
void MainWindow::createBlocks()
{
int startx = 0, starty = 0;
int WW;
int HH;
qsrand(time(NULL));
for (int i=0; i<blockcount; i++){
while(1){
WW = qrand()%10;
HH = qrand()%12;
if (blockShowW[WW]==0 && blockShowH[HH]==0){
block_[i]->move(startx+WW*50, starty+HH*50);
blockShowW[WW]=1;
blockShowH[HH]=1;
break;
}
}
}
blockTimer = new QTimer(this);
//connect(blockTimer,SIGNAL(timeout()),this,SLOT(blockAction()));
blockTValue = 8;
}
/*void MainWindow::blockAction()
{
int startx = 0, starty = 0;
int WW;
int HH;
for (int i=0; i<blockcount; i++){
while(1){
WW = qrand()%10;
HH = qrand()%12;
if (blockShowW[WW]!=0 && blockShowH[HH]!=0){
block_[i]->move(startx+WW*50, starty+HH*50);
blockShowW[WW]=1;
blockShowH[HH]=1;
break;
}
}
}
}*/
void MainWindow::gameRedy()
{
gamemod=redy;
createBlocks();
}
void MainWindow::gameLose()
{
gamemod=lose;
}
void MainWindow::gameStart()
{
gamemod=start;
//blockTimer->start(blockTValue);
}
Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QApplication>
#include <QMouseEvent>
#include <QTimer>
#include "startpage.h"
#include "plate.h";
#include "block.h";
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
int win_width=500;
int win_height=700;
protected:
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
private slots:
//void blockAction();
private:
void gameLose();
void gameStart();
void gameRedy();
void createBlocks();
enum{lose=0,start=1,redy=2};
int gamemod;
StartPage *startpage;
enum{blockcount=10};
block *block_[blockcount];
QTimer *blockTimer;
int blockTValue;
int blockShowW[11]={0};
int blockShowH[13]={0};
};
#endif // MAINWINDOW_H
block.cpp
#include "block.h"
block::block(QWidget *parent) : QWidget(parent)
{
this->setFixedSize(W,H);
this->color = (qrand()%7)+1;
}
void block::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap object;
switch(this->color){
case 1:
object.load(":/Image/blocks/1.png");
break;
case 2:
object.load(":/Image/blocks/2.png");
break;
case 3:
object.load(":/Image/blocks/3.png");
break;
case 4:
object.load(":/Image/blocks/4.png");
break;
case 5:
object.load(":/Image/blocks/5.png");
break;
case 6:
object.load(":/Image/blocks/6.png");
break;
default:
object.load(":/Image/blocks/7.png");
break;
}
painter.drawPixmap(0,0,W,H,object);
}
block.h
#ifndef BLOCK_H
#define BLOCK_H
#include <QWidget>
#include <QPainter>
#include <QPixmap>
class block : public QWidget
{
Q_OBJECT
public:
explicit block(QWidget *parent = 0);
int W = 50;
int H = 50;
private:
int color;
protected:
void paintEvent(QPaintEvent *);
};
#endif // BLOCK_H

http page isn't downloaded fully QT

Hello I wrote program that should download web page and save it as a file. It does it but partially only. Have anybody encounterd such a problem?
mainwindow.cpp source file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QNetworkAccessManager>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
netManager = new QNetworkAccessManager;
setFile("myPage");
}
MainWindow::~MainWindow()
{
netManager->deleteLater();
delete ui;
}
void MainWindow::setFile(QString fileURL)
{
QString savePath;
savePath = QString("D:/page.html");
QNetworkRequest request;
request.setUrl(QUrl(fileURL));
reply = netManager->get(request);
file = new QFile;
file->setFileName(savePath);
file->open(QIODevice::WriteOnly);
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(onProgress(qint64,qint64)));
connect(netManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
connect(reply, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished()));
}
void MainWindow::onProgress(qint64 bRead, qint64 bTotal)
{
qDebug(QString::number(bRead).toLatin1() + " - " + QString::number(bTotal).toLatin1());
}
void MainWindow::onFinished(QNetworkReply *reply)
{
switch (reply->error())
{
case QNetworkReply::NoError:
{
qDebug("File downloaded");
qDebug() << file->size();
}break;
default:
{
qDebug(reply->errorString().toLatin1());
}
}
if(file->isOpen())
{
file->close();
file->deleteLater();
}
}
void MainWindow::onReadyRead()
{
file->write(reply->readAll());
}
void MainWindow::onReplyFinished()
{
if(file->isOpen())
{
file->close();
file->deleteLater();
}
}
mainwindow.h header source file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QFile>
#include <QNetworkReply>
#include <QNetworkReply>
#include <QStringList>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void setFile(QString fileURL);
private slots:
void onFinished(QNetworkReply *);
void onProgress(qint64 bRead, qint64 bTotal);
void onReadyRead();
void onReplyFinished();
private:
Ui::MainWindow *ui;
QNetworkAccessManager *netManager;
QNetworkReply *reply;
QFile *file;
};
#endif // MAINWINDOW_H
I tried to solve it myself but after many tries I failed. I'm beginner in QT, so propably I've made a mistake somewhere, and I don't even see it. Can anybody drive me to a proper way to fix the problem?
You should put this:
connect(netManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
before:
reply = netManager->get(request);
Works fine after this modification ;)

Resources