2013-02-05 13 views
1

我在MySQl數據庫中有一個表,其中包含一個Product Category Type,SubCategory,picUrl和Price。我想將每個記錄添加到多個QGridLayout Widget中,我已將這些Ui分配給QScrollAreas以用於不同的類別和子類別。我如何添加大量的小工具到QT中的多個網格

它適用於100個項目或小數目,但如果我將LIMIT項目設置爲LIMIT 1000或任何大於100的項目,它將開始生成 QThread :: start:無法創建線程() 傳遞給C運行時函數的參數無效。錯誤。 的QThread ::開始:無法創建線程(訪問代碼無效)

有時候,我得到很多的QPixmap的比例:::像素圖是一個空像素圖 但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)); 
    } 
} 
+1

我不能在您的答案,因爲這將是很難再現,但我可以告訴你肯定分頁是更好的方式去,然後顯示1000個控件(不是用戶可見反正)。 –

+0

我可能會咬緊牙關,除非別人能夠說服我,否則這將是一個糟糕的設計,試圖將數千張圖片加載到一些佈局中。我將重新設計並使用帶有sql限制的偏移量創建下一頁,以減少每個佈局的小部件數量。 –

+0

@Nemanja Boric。你好,謝謝你的回覆。我只是在沒有閱讀你寫的內容的情況下寫了我的回覆,你的意思是我剛剛說的禮儀?創建補償和限制? –

回答

2

你想做什麼不是一個很好的方式去。即使你解決了這個錯誤--CPU和內存的消耗將會非常大,而最糟糕的部分是用戶通常無法集中注意力,甚至一次看到1000個項目,所以在大多數情況下,這是非常不必要的這是你沒有看到論壇引擎設置顯示最近10000次回覆的原因的一部分)。

您應該將MySql的LIMIT offset, number_of_items合併到窗口分頁(並且不要忘記爲用戶提供令人愉快的分頁機制)。

+1

謝謝你,我已經學到了關於人機交互(HCI)的寶貴經驗,並將在未來記住這一點。謝謝。 –

相關問題