Here we have full code of my program:

Qt Code:
  1. #ifndef NOWYWPIS_H
  2. #define NOWYWPIS_H
  3. #include <QWidget>
  4. #include <QGridLayout>
  5. #include <QLabel>
  6. #include <QComboBox>
  7. #include <QSize>
  8. QT_BEGIN_NAMESPACE
  9. class QGroupBox;
  10. class QComboBox;
  11. QT_END_NAMESPACE
  12. extern QMap<QString, QString> alkohol;
  13. extern QList<QString> ListaAl; //created in wypiszal()
  14. extern QList<QString> ListaAtr; //created in wypiszatr(QString)
  15. extern QList<QString> ListaWart; //created in wypiszwart(QString)
  16. extern void wypiszal();
  17. extern void wypiszatr(QString wybranyalk);
  18. extern void wypiszwart(QString wybranyatr);
  19. static const QSize rozmiar(100, 100);
  20.  
  21. class parametr
  22. {
  23. public:
  24. QLabel *nazwaatr;
  25. QComboBox *Combowart;
  26. };
  27. class NowyWpis : public QWidget
  28. {
  29. Q_OBJECT
  30. public:
  31. NowyWpis(QWidget *parent = 0);
  32. private slots:
  33. void AktualIkone();
  34. void AktualAtr();
  35. void AktualTr();
  36. void Dodaj();
  37. void Anuluj();
  38. void WybierzIkoneWpis();
  39. void AktualLayout(QVBoxLayout *Lay);
  40. private:
  41. void chooseImage(const QString &title, QImage *image, QToolButton *button);
  42. void loadImage(const QString &fileName, QImage *image, QToolButton *button);
  43. QVBoxLayout *Layout;
  44. QPoint imagePos(const QImage &image) const;
  45. QGroupBox *wybortr;
  46. QGroupBox *dane;
  47. QComboBox *wybierztr;
  48. QDialogButtonBox *buttonBox;
  49. QLabel *ikonatyp;
  50. QToolButton *ikonawpis;
  51. QImage sourceikonawpis;
  52. QString wybranytr;
  53. parametr wpis;
  54. QList<parametr> ListaDanych;
  55. };
To copy to clipboard, switch view to plain text mode 

#endif // NOWYWPIS_H


Qt Code:
  1. #include <QtGui>
  2. #include "nowywpis.h"
  3. NowyWpis::NowyWpis(QWidget *parent)
  4. : QWidget(parent)
  5. {
  6. Layout = new QVBoxLayout;
  7. wybortr = new QGroupBox(tr("Wybor Tr"));
  8. dane = new QGroupBox(tr("Dane"));
  9. buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok| QDialogButtonBox::Cancel);
  10. connect(buttonBox, SIGNAL(accepted()), this, SLOT(Dodaj()));
  11. connect(buttonBox, SIGNAL(rejected()), this, SLOT(Anuluj()));
  12. QGridLayout *grid = new QGridLayout;
  13. wybierztr = new QComboBox;
  14. wypiszal();
  15. foreach (QString klucz, ListaAl)
  16. {
  17. wybierztr->addItem(klucz);
  18. }
  19. connect(wybierztr, SIGNAL(currentIndexChanged(int)),this, SLOT(AktualTr()));
  20. ikonatyp = new QLabel();
  21. connect(wybierztr, SIGNAL(currentIndexChanged(int)),this, SLOT(AktualIkone()));
  22. connect(wybierztr, SIGNAL(currentIndexChanged(int)),this, SLOT(AktualAtr()));
  23. grid->addWidget(wybierztr, 0, 0);
  24. grid->addWidget(ikonatyp, 0, 1);
  25. wybortr->setLayout(grid);
  26. AktualLayout(Layout);
  27. setWindowTitle(tr("Dodaj nowy wpis"));
  28. resize(800, 600);
  29. }
  30. void NowyWpis::AktualLayout(QVBoxLayout *Lay)
  31. {
  32. //////////////////this creates Microsoft Visual C++ Library error “This application has requested the Runtime to terminate it in an unusual way.”//////////////////////////////
  33. /* while (Lay->count()>0)
  34.   {
  35.   QWidget *widget = Lay->itemAt(0)->widget();
  36.   Lay->removeWidget(widget);
  37.   delete widget;
  38.   }
  39. ////////////////////////////////////////////////////////////////////*/ Lay->addWidget(wybortr);
  40. Lay->addWidget(dane);
  41. Lay->addWidget(buttonBox);
  42. setLayout(Lay);
  43. }
  44. void NowyWpis::AktualTr()
  45. {
  46. wybranytr =(wybierztr->currentText());
  47. }
  48. void NowyWpis::AktualIkone()
  49. {
  50. ikonatyp->setAlignment(Qt::AlignBottom | Qt::AlignRight);
  51. ikonatyp->setPixmap(QPixmap("Obrazy/Tr/"+wybranytr+".png"));
  52. }
  53. void NowyWpis::AktualAtr()
  54. {
  55. QGridLayout *siatka = new QGridLayout;
  56. ListaDanych.clear();
  57. while (siatka->count()>0)
  58. {
  59. QWidget *widget = siatka->itemAt(0)->widget();
  60. siatka->removeWidget(widget);
  61. delete widget;
  62. }
  63. wypiszatr(wybranytr);
  64. foreach (QString atrybut, ListaAtr)
  65. {
  66. wpis.Combowart = new QComboBox;
  67. wpis.nazwaatr = new QLabel;
  68. wypiszwart(atrybut);
  69. wpis.nazwaatr->setText(atrybut);
  70. foreach (QString wartosc, ListaWart)
  71. {
  72. wpis.Combowart->addItem(wartosc);
  73. wpis.Combowart->setEditable(true);
  74. };
  75. ListaDanych.append(wpis);
  76. };
  77. int i=0;
  78. foreach (parametr atr, ListaDanych)
  79. {
  80. siatka->addWidget(atr.nazwaatr, i, 0);
  81. siatka->addWidget(atr.Combowart, i, 1);
  82. i++;
  83. };
  84. ikonawpis = new QToolButton();
  85. ikonawpis->setIconSize(rozmiar);
  86. connect(ikonawpis, SIGNAL(clicked()), this, SLOT(WybierzIkoneWpis()));
  87. siatka->addWidget(ikonawpis, 0, 2, 2, 1);
  88. dane->setLayout(siatka);
  89. AktualLayout(Layout);
  90. }
  91. void NowyWpis::WybierzIkoneWpis()
  92. {
  93. chooseImage(tr("Wybierz ikone"), &sourceikonawpis, ikonawpis);
  94. }
  95. void NowyWpis::chooseImage(const QString &nazwa, QImage *obraz, QToolButton *przycisk)
  96. {
  97. QString plik = QFileDialog::getOpenFileName(this, nazwa);
  98. if (!plik.isEmpty())
  99. loadImage(plik, obraz, przycisk);
  100. }
  101. void NowyWpis::loadImage(const QString &plik, QImage *obraz, QToolButton *przycisk)
  102. {
  103. obraz->load(plik);
  104. *obraz = obraz->scaled(rozmiar, Qt::KeepAspectRatio);
  105. QImage obraz2(rozmiar, QImage::Format_ARGB32_Premultiplied);
  106. QPainter painter(&obraz2);
  107. painter.drawImage(imagePos(*obraz), *obraz);
  108. painter.end();
  109. przycisk->setIcon(QPixmap::fromImage(obraz2));
  110. *obraz = obraz2;
  111. }
  112. QPoint NowyWpis::imagePos(const QImage &obraz) const
  113. {
  114. return QPoint((rozmiar.width()-obraz.width())/2,(rozmiar.height()-obraz.height())/2);
  115. }
  116. void NowyWpis::Dodaj()
  117. {
  118. }
  119. void NowyWpis::Anuluj()
  120. {
  121. }
To copy to clipboard, switch view to plain text mode 


Unfortunatelly this attempt also doesnt work. After first change of value in Combobox wibierzTr layout of groupbox dane is changing as expected (*siatka wrote in AktualAlk), but after next change of value in wybierzTr layout hasn't change and I have error:
QWidget::setLayout: Attempting to set QLayout "" on QGroupBox "", which already has a layout
I don't know how to solve it