C++ (Qt)firstLineLabel = new QLabel();
#pragma once#include "test1.h" #include <QtWidgets/QApplication> int main(int argc, char *argv[]){ QApplication a(argc, argv); test1 *w = new test1; w->setGeometry(400, 100, 700, 500); init_test(); w->show(); return a.exec();}
#pragma once#include <QtWidgets/QMainWindow>#include <qdialog>#include <QLabel>#include <QPushButton>#include <QVBoxLayout>#include <QHBoxLayout>void init_test();class test1 : public QDialog{ Q_OBJECTpublic: test1(QWidget *parent = Q_NULLPTR); QLabel *firstLineLabel[11]; // место под картинки первого ряда QHBoxLayout *firstLinelayout; QLabel *secondLineLabel[11]; // место под картинки 2 ряда QHBoxLayout *secondLinelayout ; QLabel *firstCount; QLabel *secondCount; QPushButton *push1; // кнопки QPushButton *push2; QPushButton *push3; QPushButton *push4; QPushButton *push5; QVBoxLayout *mainlayout; void Show_(QString deck[],int a, int b ) // формируем 1й лєйаут и 2й { while ((firstLinelayout->takeAt(0)) != 0) { // удаляет все єлементі из лайаута } firstLinelayout->addStretch(1); for (int i = 0; i < a; i++) { firstLineLabel[i]->setPixmap(QPixmap(deck[i])); firstLinelayout->addWidget(firstLineLabel[i]); }; firstLinelayout->addStretch(1); firstCount->setText(QString::number(a)); firstLinelayout->addWidget(firstCount); while ((secondLinelayout->takeAt(0)) != 0) { // удаляет все єлементі из лайаута } secondLinelayout->addStretch(1); for (int i = 0; i < b; i++) { secondLineLabel[i]->setPixmap(QPixmap(deck[i+ 26])); secondLinelayout->addWidget(secondLineLabel[i]); }; secondLinelayout->addStretch(1); secondCount->setText(QString::number(b)); secondLinelayout->addWidget(secondCount); }public slots: void push1Slot(); void push2Slot(); void push3Slot(); void push4Slot(); void push5Slot();private:};
#include "test1.h"#include <QString>#include <QVBoxLayout>#include <QHBoxLayout> using namespace std;int count1Line = 0; int count2Line = 0;QString deck[52];test1::test1(QWidget *parent) : QDialog(parent){ for (int i = 0; i < 11; i++) firstLineLabel[i] = new QLabel(); firstLinelayout = new QHBoxLayout; for (int i = 0; i < 11; i++) secondLineLabel[i] = new QLabel(); secondLinelayout = new QHBoxLayout; firstCount = new QLabel(); secondCount = new QLabel(); push1 = new QPushButton(" +1 pic 1st Line "); push2 = new QPushButton(" clear 1st Line "); push3 = new QPushButton(" +1 pic 2st Line "); push4 = new QPushButton(" clear 2st Line "); push5 = new QPushButton(" Random "); QHBoxLayout *pushlayout = new QHBoxLayout; pushlayout->addWidget(push1); pushlayout->addWidget(push2); pushlayout->addWidget(push3); pushlayout->addWidget(push4); pushlayout->addWidget(push5); mainlayout = new QVBoxLayout; mainlayout->addLayout(firstLinelayout); mainlayout->addLayout(secondLinelayout); mainlayout->addLayout(pushlayout); setLayout(mainlayout); connect(push1, SIGNAL(clicked()), this, SLOT(push1Slot())); connect(push2, SIGNAL(clicked()), this, SLOT(push2Slot())); connect(push3, SIGNAL(clicked()), this, SLOT(push3Slot())); connect(push4, SIGNAL(clicked()), this, SLOT(push4Slot())); connect(push5, SIGNAL(clicked()), this, SLOT(push5Slot()));}void test1::push1Slot() // добавляем 1 картинку в 1ю линию{ count1Line++; if (count1Line > 10) count1Line = 10; Show_(deck,count1Line, count2Line);}void test1::push2Slot() // очищаем 1ю линию{ count1Line=0; Show_(deck,count1Line, count2Line);}void test1::push3Slot()// добавляем 1 картинку в 2ю линию{ count2Line++; if (count2Line > 10) count2Line = 10; Show_(deck, count1Line, count2Line);}void test1::push4Slot() // очищаем 2ю линию{ count2Line = 0; Show_(deck, count1Line, count2Line);}void test1::push5Slot() // рандомное колво картинок на каждой линии{ count1Line = rand() % 10; count2Line = rand() % 10; Show_(deck, count1Line, count2Line);}void init_test() // создаем ссылки на картинки { setlocale(LC_ALL, "Russian"); int Size_; QString Res; char Card_Mast[] = { 's' , 'c', 'd', 'h' }; char Card_nominal[] = { '2','3','4','5','6','7','8','9','T','J','Q','K','A' }; for (int i = 0; i < 4; i++) for (int j = 0; j < 13; j++) { Size_ = i * 13 + j; Res = ":/test1/Resources/"; Res.append(Card_nominal[j]); Res.append(Card_Mast[i]); Res.append(".png"); deck[Size_] = Res; }}