I have a table in a MySQl database which consists of a Product Category Type, SubCategory, picUrl and a Price. I would like to add each record to multiple QGridLayout Widget's which i have made the Ui and assigned to QScrollAreas for different Categories and SubCategories.
It works fine for 100 items, or a small number, but if I set the LIMIT items to LIMIT 1000 or anything much larger then 100 .. it starts to produce QThread::start: Failed to create thread() Invalid parameter passed to C runtime function. errors. QThread::start: Failed to create thread (The access code is invalid.)
And i sometimes get lots of QPixmap::scaled: Pixmap is a null pixmap But my slot updates the Picture after a QNetworkReply is recieved so it will load on its own time.
Is this because of too many creation of classes, is there a way to slow down the creation and adding to my gridlayouts if that is the case so i am able to view them all? Or, should i redesign and create pages of 100 items and/or show a page of only 1 category instead of a TabWidget with everything?...
I really want any number of widgets inserted into my Gridlayouts.
Here is the code im using.
void checkNewArrivals::addAllItems(){
QString sql = "SELECT Category, SubCategory, picUrl, usPrice FROM newproducts LIMIT 1000";
QSqlQuery query(this->db);
query.exec(sql);
while (query.next()){
QString category = query.value(0).toString();
QString subCategory = query.value(1).toString();
QString picUrl = query.value(2).toString();
QString usPrice = query.value(3).toString();
addItem("id", picUrl, usPrice, category, subCategory);
}
}
the addItem Function
void checkNewArrivals::addItem(QString id, QString picUrl, QString price, QString category, QString subCategory){
if (category == "Autumn-Spring"){
if (subCategory == "Dress"){
addToGrid(ui->autumnSpringDressLayout, id, picUrl, price);
}else if (subCategory == "Blouse"){
addToGrid(ui->autumnSpringBlouseLayout, id, picUrl, price);
} else if (subCategory == "Long Coat"){
addToGrid(ui->autumnSpringLongCoatLayout, id, picUrl, price);
} else if (subCategory == "Short Coat"){
addToGrid(ui->autumnSpringShortCoatLayout, id, picUrl, price);
} else if (subCategory == "Sweater"){
addToGrid(ui->autumnSpringSweaterLayout, id, picUrl, price);
} else if (subCategory == "Skirt Pants"){
addToGrid(ui->autumnSpringSkirtPantsLayout, id, picUrl, price);
} else if (subCategory == "Sportsuit"){
addToGrid(ui->autumnSpringSportsuitLayout, id, picUrl, price);
} else if (subCategory == "Vest Bustier"){
addToGrid(ui->autumnSpringVestBustierLayout, id, picUrl, price);
}
}
}
function to add to grid
void checkNewArrivals::addToGrid(QGridLayout *layout, QString id, QString picUrl, QString usPrice){
checkNewArrivalItem *item = new checkNewArrivalItem;
item->setupItem(id, picUrl, usPrice);
int row = (layout->count() / 4);
int col = (layout->count() % 4);
layout->addWidget(item, row, col);
qDebug() << "Item Added to Grid: Row: " << row << " Col: " << col << endl;
}
and my checkNewArrivalitem class header
#ifndef CHECKNEWARRIVALITEM_H
#define CHECKNEWARRIVALITEM_H
#include <QWidget>
#include "filedownloader.h"
#include <QGridLayout>
namespace Ui {
class checkNewArrivalItem;
}
class checkNewArrivalItem : public QWidget
{
Q_OBJECT
public:
explicit checkNewArrivalItem(QWidget *parent = 0);
~checkNewArrivalItem();
void setupItem(QString id, QString picUrl, QString usPrice);
FileDownloader *m_pImgCtrl;
QString id, picUrl, usPrice;
QGridLayout *layout;
private slots:
void loadImage();
private:
Ui::checkNewArrivalItem *ui;
};
#endif // CHECKNEWARRIVALITEM_H
and the checkNewArrivalItem Class cpp
#include "checknewarrivalitem.h"
#include "ui_checknewarrivalitem.h"
checkNewArrivalItem::checkNewArrivalItem(QWidget *parent) :
QWidget(parent),
ui(new Ui::checkNewArrivalItem)
{
ui->setupUi(this);
}
checkNewArrivalItem::~checkNewArrivalItem()
{
delete ui;
}
void checkNewArrivalItem::setupItem(QString id, QString picUrl, QString usPrice){
ui->itemGroupBox->setTitle("ID: " + id);
ui->priceLabel->setText("US $" + usPrice);
m_pImgCtrl = new FileDownloader(QUrl(picUrl), this);
connect(m_pImgCtrl, SIGNAL(downloaded()), SLOT(loadImage()));
this->setMinimumHeight(267);
}
void checkNewArrivalItem::loadImage(){
QPixmap *buttonImage = new QPixmap;
if (!m_pImgCtrl->downloadedData().isNull()){
buttonImage->loadFromData(m_pImgCtrl->downloadedData());
int h = ui->photoLabel->height();
int w = ui->photoLabel->width();
ui->photoLabel->setPixmap(buttonImage->scaled(w, h, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
}
What are you trying to do is not a very good way to go. Even if you resolve this error - CPU and memory consumption will be very large, and the worst part is that user is not usually able to concentration, or even see 1000 items at once, so, in the most cases, it is really unnecessary (this is a part of the reason why you don't see forum engine setup to display last 10000 replies).
You should combine MySql's LIMIT offset, number_of_items
to paginate your window (and don't forget to provide pleasant pagination mechanism for user).