Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

Допиши код C++ на Builder 10.4 Rad Studio. Сделай так, чтобы при нажатии на строку в…

  1. User

    Допиши код C++ на Builder 10.4 Rad Studio. Сделай так, чтобы при нажатии на строку в StringGrid, из базы данных подгружалось image этой строки и загружалось в TImage1.

    //---------------------------------------------------------------------------

    #include <vcl.h> #pragma hdrstop

    #include "Unit9.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm9 *Form9; using namespace std;

    class CosmeticCatalog { private: TADOConnection *ADOConnection; TADOQuery *ADOQuery; TStringGrid *StringGrid; TImage *Image; public: CosmeticCatalog(TADOConnection *ADOConnection, TADOQuery *ADOQuery, TStringGrid *StringGrid, TImage *Image) { this->ADOConnection = ADOConnection; this->ADOQuery = ADOQuery; this->StringGrid = StringGrid; this->Image = Image; this->ADOQuery->Connection = this->ADOConnection; this->ADOQuery->SQL->Text = "SELECT * FROM catalog.products"; this->ADOQuery->Open(); }

    void LoadCatalog()
    {
        this->StringGrid->RowCount = this->ADOQuery->RecordCount + 1;
        this->StringGrid->ColCount = 3;
        this->StringGrid->Cells[0][0] = "Наименование";
        this->StringGrid->Cells[1][0] = "Описание";
        this->StringGrid->Cells[2][0] = "Цена";
        for(int i=1; i<=this->ADOQuery->RecordCount; i++)
        {
            this->StringGrid->Cells[0][i] = this->ADOQuery->FieldByName("name")->AsString;
            this->StringGrid->Cells[1][i] = this->ADOQuery->FieldByName("description")->AsString;
            this->StringGrid->Cells[2][i] = this->ADOQuery->FieldByName("price")->AsString;
            this->ADOQuery->Next();
        }
    }
    

    };

    CosmeticCatalog *Catalog;

    //--------------------------------------------------------------------------- __fastcall TForm9::TForm9(TComponent* Owner) : TForm(Owner) { Catalog = new CosmeticCatalog(ADOConnection1, ADOQuery1, StringGrid1, Image1); Catalog->LoadCatalog(); } //--------------------------------------------------------------------------- void __fastcall TForm9::FormCreate(TObject *Sender) { // создание базы данных / таблицы, если их ещё не существует String s; s = "CREATE DATABASE IF NOT EXISTS catalog"; ADOQuery1->SQL->Text = s; ADOQuery1->ExecSQL(); s = "CREATE TABLE IF NOT EXISTS catalog.products (ID INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) CHARACTER SET utf8mb4 NOT NULL, price FLOAT(8, 2) NOT NULL, description TEXT CHARACTER SET utf8mb4 NOT NULL, image LONGBLOB NOT NULL, PRIMARY KEY (ID))"; ADOQuery1->SQL->Text = s; ADOQuery1->ExecSQL();

    ADOQuery1->Close();
    ADOQuery1->SQL->Text = "SELECT * FROM `catalog`.`products`";
    ADOQuery1->Open();
    

    } //---------------------------------------------------------------------------

  2. Assistant

    // Обработчик события нажатия на ячейку в StringGrid void __fastcall TForm9::StringGrid1Click(TObject *Sender) { int prodId = StringGrid1->Row;

    // Загрузка изображения из базы данных для выбранной строки
    TADOQuery *imageQuery = new TADOQuery(NULL);
    imageQuery->Connection = ADOConnection1;
    imageQuery->SQL->Text = "SELECT `image` FROM `catalog`.`products` WHERE `ID` = " + IntToStr(prodId);
    imageQuery->Open();
    // Этот код: TGraphicField *imageField = (TGraphicField *)imageQuery->FieldByName("image");
    // Как и следующий, не подходит, поскольку в данном случае работы с TGraphicField
    // следует использовать TJPEGImage, а не TGraphicField
    TMemoryStream *memStream = new TMemoryStream();
    TBlobField *blobField = (TBlobField *)imageQuery->FieldByName("image");
    blobField->SaveToStream(memStream);
    memStream->Position = 0;
    TJPEGImage *jpeg = new TJPEGImage();
    jpeg->LoadFromStream(memStream);
    Image1->Picture->Assign(jpeg);
    imageQuery->Close();
    delete imageQuery;
    delete memStream;
    delete jpeg;
    

    }