Results 1 to 5 of 5

Thread: Update QScrollArea after open files

  1. #1
    Join Date
    Jul 2011
    Posts
    6
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Update QScrollArea after open files

    Hello! I need help ....
    In my program worked well, I parsed the xml file and I created the label and edit text in a ScrollArea. But I do not know how to open a file and update my ScrollArea with the new label and textedit. How do I do this??

    This is the code. Cpp thanks!

    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. desktopGeometry = QApplication::desktop()->availableGeometry(0);
    4.  
    5. scrollArea = new QScrollArea;
    6.  
    7. setCentralWidget(scrollArea);
    8. QGroupBox *groupBox = new QGroupBox(scrollArea);
    9. groupBox->setTitle(tr("Mobneu"));
    10. QGridLayout *gridLayout = new QGridLayout(groupBox);
    11. groupBox->setLayout(gridLayout);
    12.  
    13. // scrollArea->setBackgroundRole(QPalette::Dark);
    14.  
    15. QDomDocument doc("test");
    16. QFile file("test.xml");
    17. if (!file.open(QIODevice::ReadOnly))
    18. return ;
    19. if (!doc.setContent(&file)) {
    20. file.close();
    21. return ;
    22. }
    23. file.close();
    24.  
    25.  
    26. QDomElement docElem = doc.documentElement();
    27.  
    28. QDomNode n = docElem.firstChild();
    29. //QString NodeName = q.nodeName();
    30. //QString NodeValue = q.nodeValue();
    31. QLabel *label[20];
    32. QTextEdit *textEdit[20];
    33.  
    34. QLabel *labelData[20];
    35. QString srcValue;
    36.  
    37.  
    38. int count=0;
    39. int count2=0;
    40.  
    41. array2=new QVector< QString>;
    42. int cont2=0;
    43. //attr[20]=new QString;
    44.  
    45. // QVector< QVector<QString> *>vvStr;
    46. // QVector<QString> *vStr = new QVector<QString>;
    47. // QVector<QString> *Temp = new QVector<QString>;
    48. // QVector<QString> *Temp2 = new QVector<QString>;
    49. array.resize(20);
    50. for (int i = 0; i < 20; ++i)
    51. array[i].resize(20);
    52.  
    53.  
    54.  
    55. // QVector<QString> *vect;
    56. int k=0;
    57. int i=0;
    58. int ciccio=0;
    59. int cont=0;
    60.  
    61.  
    62. while(!n.isNull()) {
    63. cont=0;
    64. QDomElement e = n.toElement(); // try to convert the node to an element.
    65. if(!e.isNull()) {
    66.  
    67. label[i]=new QLabel(groupBox);
    68. label[i]->setText(qPrintable(e.tagName()));
    69. set[i]=new QString;
    70. set[i]->append(qPrintable(e.tagName())).toCaseFolded();
    71.  
    72. count++;
    73. qDebug() << qPrintable(e.tagName()) ; // the node really is an element.
    74. }
    75. QDomNode p = n.firstChild();
    76.  
    77. while(!p.isNull()) {
    78. cont++;
    79. QDomElement e = p.toElement();
    80. labelData[k]=new QLabel;
    81. labelData[k]->setText(qPrintable(e.tagName()));
    82. param[k]=new QString;
    83. param2[k]=new QString;
    84. param[k]->append(qPrintable(e.tagName()));
    85. param2[k]->append(param[k]->toLower());
    86.  
    87. if(!e.isNull()) {
    88. count2++;
    89. qDebug() << qPrintable(e.tagName()) ; // the node really is an element.
    90. }
    91. QDomNode q = p.firstChild();
    92. cont2=0;
    93. while(!q.isNull()) {
    94.  
    95. cont2++;
    96. QDomElement e = q.toElement();
    97.  
    98. srcValue=qPrintable( q.attributes().namedItem( "value" ).nodeValue());
    99.  
    100. // wid->append(srcValue);
    101.  
    102. textEdit[ciccio]=new QTextEdit;
    103. textEdit[ciccio]->setMaximumSize(150,70);
    104. textEdit[ciccio]->append(srcValue);
    105.  
    106. QString qString2 = QString::fromUtf8(srcValue);
    107. attr[ciccio]=new QString;
    108. attr[ciccio]->append(qString2);
    109.  
    110.  
    111. if(!e.isNull()) {
    112.  
    113. array2->insert(ciccio,srcValue);
    114.  
    115. array[k][ciccio]=srcValue;
    116. //vStr->append(srcValue);
    117.  
    118.  
    119.  
    120. // qDebug() << srcValue ; // the node really is an element.
    121. // qDebug()<<vStr->at(k);
    122.  
    123. }
    124.  
    125. // qDebug() << k ;
    126.  
    127. ciccio++;
    128.  
    129.  
    130. q=q.nextSibling();
    131.  
    132.  
    133.  
    134. }
    135.  
    136. // qDebug() << vStr->at(k);
    137.  
    138. //qDebug() << count;
    139.  
    140. p = p.nextSibling();
    141.  
    142.  
    143.  
    144. qDebug() << cont2;
    145. conteggio2[k]=cont2;
    146. k++;
    147.  
    148. }
    149.  
    150. conteggio[i]=cont;
    151. i++;
    152. parametr=i;
    153. //qDebug()<< i;
    154. n = n.nextSibling();
    155. // count=0;
    156. }
    157. // qDebug()<<vStr->count();
    158. // qDebug()<<vvStr.count();
    159.  
    160. // qDebug()<<Temp2->at(0);
    161. //qDebug()<<Temp->at(1);
    162. // qDebug()<<Temp2->at(1);
    163. // QVBoxLayout *lay=new QVBoxLayout(scrollArea);
    164.  
    165.  
    166. qDebug()<<conteggio2[0];
    167. qDebug()<<conteggio2[1];
    168. qDebug()<<conteggio2[2];
    169.  
    170. int h=0;
    171. int f=0;
    172. int u=0;
    173. int o=0;
    174. int p=0;
    175.  
    176.  
    177. for (int i = 0;i <parametr; i++) {
    178. if(o!=0){
    179. o=o+1;
    180. }
    181. gridLayout->addWidget(label[i],i+h,0);
    182. for ( int j=0 ;j <conteggio[i]; j++){
    183.  
    184. gridLayout->addWidget(labelData[h],f+1,1);
    185. h++;
    186. f++;
    187.  
    188. for(int s=0;s<conteggio2[p];s++){
    189.  
    190. gridLayout->addWidget(textEdit[u],o+1,s+2);
    191. u++;
    192.  
    193.  
    194. }
    195. o++;
    196. //u=0;
    197. //u=u+conteggio2[j];
    198. p++;
    199. }
    200.  
    201. // h=0;
    202.  
    203. // h=h+conteggio[i];
    204. f=f+1;
    205. //gridLayout->addWidget(textEdit[i][j],j+1,2);
    206.  
    207. //lay.addWidget(ses,i,0);
    208.  
    209.  
    210. }
    211.  
    212.  
    213.  
    214.  
    215.  
    216. for(int i=0;i<count;i++)
    217. qDebug()<<set[i]->toCaseFolded();
    218. for(int i=0;i<count2;i++)
    219. qDebug()<<param[i]->toLower();
    220.  
    221. for(int i=0;i<3;i++){
    222. for(int j=0;j<5;j++){
    223. qDebug()<<array[i][j];
    224. }
    225.  
    226. }
    227. for(int i=0;i<2;i++)
    228. qDebug()<<conteggio[i];
    229. qDebug()<<conteggio2[2];
    230.  
    231. for(int i=0;i<2;i++)
    232. qDebug()<<param2[i];
    233.  
    234. /*
    235. nlayLabel = new QLabel(tr("NLAYER ="));
    236. numnoLabel = new QLabel(tr("NUMNODI ="));
    237. dlayLabel = new QLabel(tr("DISPLAYER ="));
    238. domainLabel = new QLabel(tr("DOMAIN ="));
    239. etaeLabel = new QLabel(tr("ETAERR ="));
    240. etathLabel = new QLabel(tr("ETATHETA ="));
    241. etaLabel1 = new QLabel(tr("ETA1 ="));
    242. etaLabel2 = new QLabel(tr("ETA2 ="));
    243. etaLabel3 = new QLabel(tr("ETA3 ="));
    244. */
    245. // gridLayout->addWidget(nlayLabel,3,0);
    246. // gridLayout->addWidget(numnoLabel,4,0);
    247. // gridLayout->addWidget(dlayLabel,5,0);
    248.  
    249. if (desktopGeometry.height() < 400){
    250. gridLayout->setVerticalSpacing(80);
    251. }
    252. else{
    253. gridLayout->setVerticalSpacing(10); //150
    254. }
    255.  
    256. // gridLayout->addWidget(label);
    257. // gridLayout->addWidget(lineEdit);
    258. // gridLayout->addWidget(button);
    259.  
    260. scrollArea->setWidget(groupBox);
    261. scrollArea->setGeometry(20,30,700,600);
    262. QHBoxLayout* layout = new QHBoxLayout();
    263. layout->addWidget(scrollArea,2,0);
    264. // setLayout(layout);
    265. scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    266. scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    267.  
    268.  
    269. //scrollArea->setWidgetResizable(true);
    270.  
    271.  
    272. connect(QApplication::desktop(), SIGNAL(workAreaResized(int)),
    273. this, SLOT(desktopResized(int)));
    274.  
    275. // lay->addWidget(scrollArea);
    276. // setLayout(lay);
    277.  
    278. setStyleSheet("QAbstractScrollArea { margin: 20px; }");
    279. createActions();
    280. createMenus();
    281.  
    282.  
    283.  
    284. setWindowTitle(tr("XML file"));
    285. resize(800, 800);
    286.  
    287. }
    288.  
    289. void MainWindow::desktopResized(int screen)
    290. {
    291. if (screen != 0)
    292. return;
    293. reactToSIP();
    294. }
    295.  
    296. void MainWindow::reactToSIP()
    297. {
    298. QRect availableGeometry = QApplication::desktop()->availableGeometry(0);
    299.  
    300. if (desktopGeometry != availableGeometry) {
    301. if (windowState() | Qt::WindowMaximized)
    302. setWindowState(windowState() & ~Qt::WindowMaximized);
    303.  
    304.  
    305.  
    306. setGeometry(availableGeometry);
    307. }
    308.  
    309. desktopGeometry = availableGeometry;
    310. }
    311.  
    312. void MainWindow::open()
    313. {
    314. QString fileName =
    315. QFileDialog::getOpenFileName(this, tr("Open xml File"),
    316. QDir::currentPath(),
    317. tr("XML Files (*.xml)"));
    318. if (fileName.isEmpty())
    319. return;
    320.  
    321. QFile file(fileName);
    322. if (!file.open(QFile::ReadOnly | QFile::Text)) {
    323. QMessageBox::warning(this, tr("xml"), tr("Cannot read file %1:\n%2.")
    324. .arg(fileName)
    325. .arg(file.errorString()));
    326. return;
    327. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Update QScrollArea after open files

    But I do not know how to open a file and update my ScrollArea with the new label and textedit.
    You are opening a file at least in two places in your code.
    What exactly is the problem you have?
    Please be specific.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2011
    Posts
    6
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Update QScrollArea after open files

    I would just open new file from the menu.
    Thank you so much

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Update QScrollArea after open files

    But what is the problem you have?
    It seems you know how to open a file...!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jul 2011
    Posts
    6
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Update QScrollArea after open files

    my problem was that it did not update the scroll area when I opened a new file ...
    But now everything seems to work ... Thanks for your interest ..

Similar Threads

  1. Replies: 6
    Last Post: 13th August 2011, 18:31
  2. Open odt files with Qt
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2011, 09:29
  3. qca, qca-gnupg Not able to open files
    By sky in forum Newbie
    Replies: 1
    Last Post: 15th February 2011, 06:11
  4. open files in different windows
    By ramaha8319 in forum Qt Tools
    Replies: 1
    Last Post: 27th April 2009, 14:27
  5. [Solved] Open files outside my program
    By Nykoo in forum Qt Programming
    Replies: 0
    Last Post: 27th January 2008, 16:13

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.