我在 MySQl 数据库中有一个表,它由产品类别类型、子类别、picUrl 和价格组成。我想将每条记录添加到多个 QGridLayout 小部件中,我已经制作了 Ui 并分配给 QScrollAreas 以用于不同的类别和子类别。
它适用于 100 个项目或少量项目,但如果我将 LIMIT 项目设置为 LIMIT 1000 或任何大于 100 的项目 .. 它开始产生 QThread::start: Failed to create thread() Invalid parameter passing to C运行时函数。错误。QThread::start: 创建线程失败(访问码无效。)
我有时会得到很多 QPixmap::scaled: Pixmap 是一个空像素图但是我的插槽在收到 QNetworkReply 后会更新图片,因此它会在自己的时间加载。
这是因为创建的类太多,如果是这样的话,有没有办法减慢创建速度并添加到我的网格布局中,这样我就可以查看它们了?或者,我应该重新设计和创建包含 100 个项目的页面和/或仅显示 1 个类别的页面而不是包含所有内容的 TabWidget?...
我真的想要在我的 Gridlayouts 中插入任意数量的小部件。
这是我使用的代码。
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);
}
}
addItem 函数
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);
}
}
}
添加到网格的功能
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;
}
和我的 checkNewArrivalitem 类头
#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
和 checkNewArrivalItem 类 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));
}
}