PDA

View Full Version : Update QScrollArea after open files



beppuz
25th August 2011, 15:43
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!




MainWindow::MainWindow()
{
desktopGeometry = QApplication::desktop()->availableGeometry(0);

scrollArea = new QScrollArea;

setCentralWidget(scrollArea);
QGroupBox *groupBox = new QGroupBox(scrollArea);
groupBox->setTitle(tr("Mobneu"));
QGridLayout *gridLayout = new QGridLayout(groupBox);
groupBox->setLayout(gridLayout);

// scrollArea->setBackgroundRole(QPalette::Dark);

QDomDocument doc("test");
QFile file("test.xml");
if (!file.open(QIODevice::ReadOnly))
return ;
if (!doc.setContent(&file)) {
file.close();
return ;
}
file.close();


QDomElement docElem = doc.documentElement();

QDomNode n = docElem.firstChild();
//QString NodeName = q.nodeName();
//QString NodeValue = q.nodeValue();
QLabel *label[20];
QTextEdit *textEdit[20];

QLabel *labelData[20];
QString srcValue;


int count=0;
int count2=0;

array2=new QVector< QString>;
int cont2=0;
//attr[20]=new QString;

// QVector< QVector<QString> *>vvStr;
// QVector<QString> *vStr = new QVector<QString>;
// QVector<QString> *Temp = new QVector<QString>;
// QVector<QString> *Temp2 = new QVector<QString>;
array.resize(20);
for (int i = 0; i < 20; ++i)
array[i].resize(20);



// QVector<QString> *vect;
int k=0;
int i=0;
int ciccio=0;
int cont=0;


while(!n.isNull()) {
cont=0;
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull()) {

label[i]=new QLabel(groupBox);
label[i]->setText(qPrintable(e.tagName()));
set[i]=new QString;
set[i]->append(qPrintable(e.tagName())).toCaseFolded();

count++;
qDebug() << qPrintable(e.tagName()) ; // the node really is an element.
}
QDomNode p = n.firstChild();

while(!p.isNull()) {
cont++;
QDomElement e = p.toElement();
labelData[k]=new QLabel;
labelData[k]->setText(qPrintable(e.tagName()));
param[k]=new QString;
param2[k]=new QString;
param[k]->append(qPrintable(e.tagName()));
param2[k]->append(param[k]->toLower());

if(!e.isNull()) {
count2++;
qDebug() << qPrintable(e.tagName()) ; // the node really is an element.
}
QDomNode q = p.firstChild();
cont2=0;
while(!q.isNull()) {

cont2++;
QDomElement e = q.toElement();

srcValue=qPrintable( q.attributes().namedItem( "value" ).nodeValue());

// wid->append(srcValue);

textEdit[ciccio]=new QTextEdit;
textEdit[ciccio]->setMaximumSize(150,70);
textEdit[ciccio]->append(srcValue);

QString qString2 = QString::fromUtf8(srcValue);
attr[ciccio]=new QString;
attr[ciccio]->append(qString2);


if(!e.isNull()) {

array2->insert(ciccio,srcValue);

array[k][ciccio]=srcValue;
//vStr->append(srcValue);



// qDebug() << srcValue ; // the node really is an element.
// qDebug()<<vStr->at(k);

}

// qDebug() << k ;

ciccio++;


q=q.nextSibling();



}

// qDebug() << vStr->at(k);

//qDebug() << count;

p = p.nextSibling();



qDebug() << cont2;
conteggio2[k]=cont2;
k++;

}

conteggio[i]=cont;
i++;
parametr=i;
//qDebug()<< i;
n = n.nextSibling();
// count=0;
}
// qDebug()<<vStr->count();
// qDebug()<<vvStr.count();

// qDebug()<<Temp2->at(0);
//qDebug()<<Temp->at(1);
// qDebug()<<Temp2->at(1);
// QVBoxLayout *lay=new QVBoxLayout(scrollArea);


qDebug()<<conteggio2[0];
qDebug()<<conteggio2[1];
qDebug()<<conteggio2[2];

int h=0;
int f=0;
int u=0;
int o=0;
int p=0;


for (int i = 0;i <parametr; i++) {
if(o!=0){
o=o+1;
}
gridLayout->addWidget(label[i],i+h,0);
for ( int j=0 ;j <conteggio[i]; j++){

gridLayout->addWidget(labelData[h],f+1,1);
h++;
f++;

for(int s=0;s<conteggio2[p];s++){

gridLayout->addWidget(textEdit[u],o+1,s+2);
u++;


}
o++;
//u=0;
//u=u+conteggio2[j];
p++;
}

// h=0;

// h=h+conteggio[i];
f=f+1;
//gridLayout->addWidget(textEdit[i][j],j+1,2);

//lay.addWidget(ses,i,0);


}





for(int i=0;i<count;i++)
qDebug()<<set[i]->toCaseFolded();
for(int i=0;i<count2;i++)
qDebug()<<param[i]->toLower();

for(int i=0;i<3;i++){
for(int j=0;j<5;j++){
qDebug()<<array[i][j];
}

}
for(int i=0;i<2;i++)
qDebug()<<conteggio[i];
qDebug()<<conteggio2[2];

for(int i=0;i<2;i++)
qDebug()<<param2[i];

/*
nlayLabel = new QLabel(tr("NLAYER ="));
numnoLabel = new QLabel(tr("NUMNODI ="));
dlayLabel = new QLabel(tr("DISPLAYER ="));
domainLabel = new QLabel(tr("DOMAIN ="));
etaeLabel = new QLabel(tr("ETAERR ="));
etathLabel = new QLabel(tr("ETATHETA ="));
etaLabel1 = new QLabel(tr("ETA1 ="));
etaLabel2 = new QLabel(tr("ETA2 ="));
etaLabel3 = new QLabel(tr("ETA3 ="));
*/
// gridLayout->addWidget(nlayLabel,3,0);
// gridLayout->addWidget(numnoLabel,4,0);
// gridLayout->addWidget(dlayLabel,5,0);

if (desktopGeometry.height() < 400){
gridLayout->setVerticalSpacing(80);
}
else{
gridLayout->setVerticalSpacing(10); //150
}

// gridLayout->addWidget(label);
// gridLayout->addWidget(lineEdit);
// gridLayout->addWidget(button);

scrollArea->setWidget(groupBox);
scrollArea->setGeometry(20,30,700,600);
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(scrollArea,2,0);
// setLayout(layout);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded );
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);


//scrollArea->setWidgetResizable(true);


connect(QApplication::desktop(), SIGNAL(workAreaResized(int)),
this, SLOT(desktopResized(int)));

// lay->addWidget(scrollArea);
// setLayout(lay);

setStyleSheet("QAbstractScrollArea { margin: 20px; }");
createActions();
createMenus();



setWindowTitle(tr("XML file"));
resize(800, 800);

}

void MainWindow::desktopResized(int screen)
{
if (screen != 0)
return;
reactToSIP();
}

void MainWindow::reactToSIP()
{
QRect availableGeometry = QApplication::desktop()->availableGeometry(0);

if (desktopGeometry != availableGeometry) {
if (windowState() | Qt::WindowMaximized)
setWindowState(windowState() & ~Qt::WindowMaximized);



setGeometry(availableGeometry);
}

desktopGeometry = availableGeometry;
}

void MainWindow::open()
{
QString fileName =
QFileDialog::getOpenFileName(this, tr("Open xml File"),
QDir::currentPath(),
tr("XML Files (*.xml)"));
if (fileName.isEmpty())
return;

QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, tr("xml"), tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}

high_flyer
26th August 2011, 09:45
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.

beppuz
26th August 2011, 13:22
I would just open new file from the menu.
Thank you so much

high_flyer
26th August 2011, 13:40
But what is the problem you have?
It seems you know how to open a file...!

beppuz
26th August 2011, 15:00
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 ..:)