PDA

View Full Version : Scrollable window



Rakma74
12th August 2012, 13:57
Dear Sirs,

I'm designing a window which appears when information has to be entered.
This window contains a variable number of ComboBox, but when this number is to big, the window size is bigger than the screen size.

I tried to implement a scroll bar (last lines of the code bellow), but it freezes the program ??

Would someone have any idea ?

Thanks in advance,

Best Regards,

Stéphane





// Telemetry data assignment
void MainWindow::data_assignment()
{
// New window object instanciation
window_data_assignment = new QWidget;

...
...
...
...

// Data input assigment sub-widget creation
QWidget *data_assigment_input;
data_assigment_input = new QWidget;

// Label displayed of the combobox
QLabel *Label;
// Combo box // if column_number if a constant
// QComboBox *ComboBox[columns_number];

// Combo box // if column_number if a constant
// this will have to be release by using delete []comboBox;
QComboBox** ComboBox = new QComboBox*[columns_number];
// delete []comboBox;


// GRID LAYOUT declaration
QGridLayout *grid;


// GRID LAYOUT INSTANTIATION
grid = new QGridLayout;

// Data assigmnent window filling with input sub-widgets
for (unsigned int row = 0; row < columns_number; row++)
{
Label = new QLabel(tr("Data type of column # ")+QString::number(row+1)+" :");

ComboBox[row] = new QComboBox;

// Combo box data filling
for (unsigned int i = 0; i<DATA_TYPE_NB; i++)
ComboBox[row]->addItem(tr(DATA_TYPE[i]));

grid->addWidget(Label, row, 0);
grid->addWidget(ComboBox[row], row, 1);
}

//
data_assigment_input->setLayout(grid);



// Quit push button creation
QPushButton *quit = new QPushButton(tr("OK"));
// quit->setFont(QFont("Times", 18, QFont::Bold));

// Connection between widgets
connect(quit, SIGNAL(clicked()), window_data_assignment, SLOT(close()));

// Size/Position auto-managment by QVBoxLayout
QVBoxLayout *wlayout = new QVBoxLayout;
wlayout->addWidget(data_assigment_input);
wlayout->addWidget(quit);



// Window scrolling
// Adding a scroll area
QScrollArea *scrollArea = new QScrollArea;
// scrollArea->setWidgetResizable(true);
scrollArea->setWidget(window_data_assignment);

wlayout->addWidget(scrollArea);

window_data_assignment->setLayout(wlayout);

}

Lykurg
12th August 2012, 15:42
Hi,

see QScrollArea. It should solve your problem.

Best,
Lykurg

Rakma74
12th August 2012, 17:50
Dear Lykurg,

Thank you very much for your answer !!!

This is what I did with the following code (see bellow), but unfortunately without success :'(

Best Regards,

Stéphane

68. // Window scrolling
69. // Adding a scroll area
70. QScrollArea *scrollArea = new QScrollArea;
71. // scrollArea->setWidgetResizable(true);
72. scrollArea->setWidget(window_data_assignment);
73.
74. wlayout->addWidget(scrollArea);
75.
76. window_data_assignment->setLayout(wlayout);

Lykurg
12th August 2012, 21:08
Of course it does not work! Your code is nonsense. :)


You assign window_data_assignment to the scroll area. That is fine. Then you add the scroll area to a layout. That is also fine, but then you add the layout to window_data_assignment... Why?

Rakma74
13th August 2012, 09:58
Thank you so much for your answer, and you are so right, sorry for this nonsense ;)

I managed to solve my problem, with the following code (sorry, I didn't find the 'insert code' button), thanks again for you help !!

Best Regards,

Stéphane


// Global layout declaration (window_data_assignment)
QVBoxLayout * vLayout = new QVBoxLayout(window_data_assignment);
// ScrollArea declaration (window_data_assignment)
QScrollArea * scrollArea = new QScrollArea(window_data_assignment);

// ScrollArea properties definition
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded );
scrollArea->setWidgetResizable(false);

// Assigment of data_assigment_input to ScrollArea
scrollArea->setWidget(data_assigment_input);

// Assigment of ScrollArea to the global layout
vLayout->addWidget(scrollArea);

// Assigment of the global layout to the current window widget
window_data_assignment->setLayout(vLayout);