Results 1 to 8 of 8

Thread: QGridLayout refresh

  1. #1
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    10

    Default QGridLayout refresh

    Hi, I'm trying to change grid layout in one of the GroupBoxes, where layout depends on option chosen in Combobox. Here is shortened version of my program. funcion AktualA should change layout in groupBox dane, but this works only with first change of Combobox. Thanks for help

    nowywpis.h

    Qt Code:
    1. #ifndef NOWYWPIS_H
    2. #define NOWYWPIS_H
    3. #include <QWidget>
    4. #include <QGridLayout>
    5. #include <QComboBox>
    6. QT_BEGIN_NAMESPACE
    7. class QGroupBox;
    8. class QComboBox;
    9. QT_END_NAMESPACE
    10.  
    11. class NowyWpis : public QWidget
    12. {
    13. Q_OBJECT
    14. public:
    15. NowyWpis(QWidget *parent = 0);
    16. private slots:
    17. void AktualA();
    18. void AktualT();
    19. private:
    20. void WyborT();
    21. QGroupBox *wybort;
    22. QGroupBox *dane;
    23. QComboBox *wybierzt;
    24. };
    25. #endif // NOWYWPIS_H
    To copy to clipboard, switch view to plain text mode 

    nowywpis.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "nowywpis.h"
    3. NowyWpis::NowyWpis(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. WyborT();
    7. }
    8. void NowyWpis::WyborT()
    9. {
    10. QVBoxLayout *Layout = new QVBoxLayout;
    11. wybort = new QGroupBox(tr("Wybor T"));
    12. dane = new QGroupBox(tr("Dane"));
    13. QGridLayout *grid = new QGridLayout;
    14. wybierzt = new QComboBox;
    15. …
    16. connect(wybierzt, SIGNAL(currentIndexChanged(int)),
    17. this, SLOT(AktualT()));
    18. connect(wybierzt, SIGNAL(currentIndexChanged(int)),
    19. this, SLOT(AktualA()));
    20. …
    21. grid->addWidget(wybierzt, 0, 0);
    22. wybort->setLayout(grid);
    23. Layout->addWidget(wybort);
    24. Layout->addWidget(dane);
    25. setLayout(Layout);
    26. setWindowTitle(tr("Dodaj nowy wpis"));
    27. resize(800, 600);
    28. }
    29.  
    30. void NowyWpis::AktualT()
    31. {
    32. wybranyt =(wybierzt->currentText());
    33. }
    34.  
    35. void NowyWpis::AktualA()
    36. {
    37.  
    38. QGridLayout *siatka = new QGridLayout;
    39. …
    40. dane->setLayout(siatka);
    41. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGridLayout refresh

    What about it doesn't work? How do you know it isn't working?

  3. #3
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    10

    Default Re: QGridLayout refresh

    When I run program i have empty groupbox dane. After first change value in Combobox wibierzT layout of groupbox is changing as expected (*siatka wrote in AktualA), but after next change of value in wybierzT layout hasn't change

  4. #4
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    10

    Default Re: QGridLayout refresh

    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

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGridLayout refresh

    Quote Originally Posted by Pawello View Post
    QWidget::setLayout: Attempting to set QLayout "" on QGroupBox "", which already has a layout
    I don't know how to solve it
    The error message is self-explanatory. Hint: read the documentation of the method it mentions.

  6. #6
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    10

    Default Re: QGridLayout refresh

    Quote Originally Posted by yeye_olive View Post
    The error message is self-explanatory. Hint: read the documentation of the method it mentions.
    I understand that this problem is connected with setLayout but this knowledge doesn't help me to solve it. Please help me to do this.

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGridLayout refresh

    The documentation for QWidget::setLayout() -- have you read it? -- says that you must first delete a widget's layout before you can set another layout. You get the error message because you have not done so.

  8. The following user says thank you to yeye_olive for this useful post:

    Pawello (11th August 2012)

  9. #8
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    10

    Default Re: QGridLayout refresh

    I've already find it out but thanks for help

Similar Threads

  1. Refresh Ui...?
    By steve.bush in forum Newbie
    Replies: 5
    Last Post: 21st March 2011, 05:21
  2. Replies: 2
    Last Post: 14th January 2011, 15:09
  3. Trying to refresh a QGridLayout
    By zlacelle in forum Qt Programming
    Replies: 5
    Last Post: 5th April 2009, 21:21
  4. Refresh TableModel
    By abbapatris in forum Qt Programming
    Replies: 8
    Last Post: 7th March 2008, 13:55
  5. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 13:01

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.