PDA

View Full Version : Dynamic QscrollArea



beppuz
19th July 2011, 14:30
Hello guys I have a problem ..
When I want to enter new parameters in the Qscroll area, put out from the new parameters and do not see the QscrollBar. I wish it were all dynamic, that when I post a new parameter, inserts it into the area by changing the bar ... how do I?? help!!

file .h


class ParamPage : public QWidget
{
Q_OBJECT
public:
ParamPage(QWidget *parent = 0);
QString text;
QString prev;
QLabel *label;
QLineEdit *lineEdit;
QPushButton *parButton;
QPushButton *modButton;
QLabel *pathLabel;
QLineEdit *fileNameEdit;
int num;
QLineEdit *edit[10];
int lineNumber;
QStringList listFileData;
QScrollArea *scrollArea;
QRect desktopGeometry;
void writeToFile();
void reactToSIP();

private slots:
void paramClicked();
void modifClicked();
void desktopResized(int screen);


};



#endif





file .cpp


ParamPage::ParamPage(QWidget *parent)
: QWidget(parent)
{

desktopGeometry = QApplication::desktop()->availableGeometry(0);
scrollArea = new QScrollArea(this);

QGroupBox *configGroup = new QGroupBox(scrollArea);
QGridLayout *gridLayout = new QGridLayout(configGroup);
configGroup->setLayout(gridLayout);

parButton = new QPushButton(configGroup);
parButton->setText("new_par");
modButton = new QPushButton(configGroup);
modButton->setText("add par");
pathLabel = new QLabel(configGroup);
pathLabel->setText("File");
fileNameEdit = new QLineEdit(configGroup);
// QLabel *serverLabel = new QLabel(tr("Server:"));

//QComboBox *serverCombo = new QComboBox;
//serverCombo->addItem(tr("Trolltech (Australia)"));
//serverCombo->addItem(tr("Trolltech (Germany)"));
//serverCombo->addItem(tr("Trolltech (Norway)"));
//serverCombo->addItem(tr("Trolltech (People's Republic of China)"));
//serverCombo->addItem(tr("Trolltech (USA)"));
gridLayout->addWidget( parButton);
gridLayout->addWidget(modButton);
gridLayout->addWidget(pathLabel);
gridLayout->addWidget(fileNameEdit);

scrollArea->setWidget(configGroup);
QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(scrollArea);
setLayout(layout);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);



//serverLayout->addWidget(serverCombo);

connect(parButton, SIGNAL (clicked()), this, SLOT( paramClicked()));
connect(modButton, SIGNAL (clicked()), this, SLOT( modifClicked()));

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


}

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

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

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



setGeometry(availableGeometry);
}

desktopGeometry = availableGeometry;
}

void ParamPage::paramClicked(){
bool ok;
text = QInputDialog::getText(scrollArea, tr("Nuovo parametro"),
tr("Nome parametro:"), QLineEdit::Normal,QDir::home().dirName(),
&ok);

num = QInputDialog::getInteger(scrollArea, tr("Numero colonne"),tr("Percentage:"), 25, 0, 100, 1, &ok);

// QVBoxLayout *mainLayout = new QVBoxLayout;

// QLabel* l = new QLabel("new label", this);
// mainLayout->addWidget(l,1,0);
if(ok){

label = new QLabel(scrollArea);
//label->adjustSize();
//label->setIndent(15);
//this->layout()->set

this->layout()->addWidget(label);
label->setText(text);
for(int i;i<num;i++){
edit[i]=new QLineEdit(scrollArea);
this->layout()->addWidget(edit[i]);
}


// mainLayout->addWidget(label,1,0);

// mainLayout->addWidget(label,3,0);
// setLayout(mainLayout);

//lineEdit = new QLineEdit(this);

//lineEdit->set
// this->layout()->addWidget(lineEdit);




//layout()->addWidget(lineEdit);
}


}


void ParamPage::modifClicked(){
QString fileName =
QFileDialog::getOpenFileName(this, tr("Open CFG File"),
QDir::currentPath(),
tr("CFG Files ( *.cfg)"));
QFileInfo fileInfo(fileName);


fileNameEdit->setText(fileInfo.absoluteFilePath());
//pathValueLabel = new QLabel(fileInfo.fileName());
// pathValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return ;
QTextStream out(&file);
prev="";
lineNumber=0;
while (!file.atEnd()) {
QString line = file.readLine();
listFileData.append(line);
if(line.startsWith("Fine File")){
//line=prev;

QStringList list;
list = line.split(QRegExp("\\s+"));
QString str;
str.append("Exec ");
str.append("/CODE/ScriptFiles/BatchModeRunning/");
str.append("\n");
str.append("Fine File");
str.append(list[1]);
listFileData.replace(lineNumber,str);



}
// else{
// prev= line;
// }
lineNumber ++;
}
writeToFile();
}

void ParamPage::writeToFile(){

QFile file("Config_new.cfg");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;

QTextStream out(&file);
for(int i = 0; i<listFileData.size(); i++)
out << listFileData.at(i) << "\n";
}


thankss!!!!

sakya
19th July 2011, 14:48
I don't have time to look at your code, but this code works fine.

I created a QMainwindow with just the scrollArea and a button.


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QFrame>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

m_Frame = new QFrame();
ui->scrollArea->setWidget(m_Frame);
QVBoxLayout* l = new QVBoxLayout();
m_Frame->setLayout(l);

connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::onButtonClicked()
{
QPushButton* b = new QPushButton(this);
m_Frame->layout()->addWidget(b);
}

Santosh Reddy
19th July 2011, 15:26
remove this line

setLayout(layout);