Can't set ScrollBar in QScrollArea
-
Hello everybody,
I'm trying to create a scrollbar in a QScrollArea widget but I can't move the scrollbar. For more information, I'm trying to create a scrollbar as needed : when the numbers of widgets can't be displayed (with their initial size) in the QScrollArea (this is in function of TX_DISPLAY_NUMBER value...)
There is the code I tried to make:
My .h file
@#define TX_DISPLAY_NUMBER 30
#include <QScrollArea>
#include <QLineEdit>
#include <QSpinBox>
#include <QCheckBox>class TxPage : public QWidget
{
Q_OBJECT
QScrollArea *txDisplayArea;QLineEdit *tx_Label[TX_DISPLAY_NUMBER]; QLineEdit *tx_Channel[TX_DISPLAY_NUMBER]; QSpinBox *tx_SDI[TX_DISPLAY_NUMBER]; QLineEdit *tx_Data[TX_DISPLAY_NUMBER]; QSpinBox *tx_SSM[TX_DISPLAY_NUMBER]; QCheckBox *tx_Parity[TX_DISPLAY_NUMBER]; QLineEdit *tx_ReceptTime[TX_DISPLAY_NUMBER]; QLineEdit *tx_Status[TX_DISPLAY_NUMBER];
public:
TxPage();
}
@My .cpp file (in constructor)
@txDisplayArea = new QScrollArea;
for(int indexStatus = 0; indexStatus < TX_DISPLAY_NUMBER; indexStatus++){
/* Label instantiating */
tx_Label[indexStatus] = new QLineEdit(txDisplayArea);
tx_Label[indexStatus]->setEnabled(false);
tx_Label[indexStatus]->setMaxLength(4);
tx_Label[indexStatus]->setFixedSize(50,20);/* Channel instantiating */ tx_Channel[indexStatus] = new QLineEdit(txDisplayArea); tx_Channel[indexStatus]->setEnabled(false); tx_Channel[indexStatus]->setFixedSize(50,20); tx_Channel[indexStatus]->setMaxLength(2); etc.
}
QGridLayout *tx_ScrollAreaLayout = new QGridLayout;
/* Loop for Tx Display Area Layout */
for(int indexLayout = 0; indexLayout < TX_DISPLAY_NUMBER; indexLayout++){
tx_ScrollAreaLayout->addWidget(tx_Label[indexLayout] , indexLayout+1 , 0, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_Channel[indexLayout] , indexLayout+1 , 1, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_SDI[indexLayout] , indexLayout+1 , 2, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_Data[indexLayout] , indexLayout+1 , 3, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_SSM[indexLayout] , indexLayout+1 , 4, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_Parity[indexLayout] , indexLayout+1 , 5, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_Status[indexLayout] , indexLayout+1 , 6, Qt::AlignLeft);
}
txDisplayArea->setWidgetResizable(true);
txDisplayArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);txDisplayArea->setLayout(tx_ScrollAreaLayout);@
The problem is that all widgets are shown but they overlap each other and ScrollArea (txDisplayArea) isn't resized...
So, I tried (by replacing) the loop for Tx Display Area with this loop :
@ for(int indexLayout = 0; indexLayout < TX_DISPLAY_NUMBER; indexLayout++){
txDisplayArea->setWidget(tx_Label[indexLayout]);
txDisplayArea->setWidget(tx_Channel[indexLayout]);
txDisplayArea->setWidget(tx_SDI[indexLayout]);
txDisplayArea->setWidget(tx_Data[indexLayout]);
txDisplayArea->setWidget(tx_SSM[indexLayout]);
txDisplayArea->setWidget(tx_Parity[indexLayout]);
txDisplayArea->setWidget(tx_Status[indexLayout]);
}@But there is only one widget displayed on top left of the scroll area (i think it's tx_Label QLineEdit or all widgets are stacked...)
Believe me, I tried to find out the solution on my way and with our best friend : Google... and I didn't find the solution to my problem... So I'll give up, I'm desperate...
Somebody can help me ?
-
The problem is the last block.
you set a widget, and the replace it again. (setWidget does not add a widget!)if you "look at the QScrollArea docs,":http://doc.qt.nokia.com/4.7/qscrollarea.html you find
bq. A scroll area is used to display the contents of a child widget within a frame.
I interpret that (never tried it) that you must create one child widget which contains the layout and all the widghets and add that with addWidget. Don't put a layout directly onto the scroll view.
By the way, it looks like you want to create a table, why don't you use a table view and a delegate for the editing?
-
[quote author="Gerolf" date="1322777479"]why don't you use a table view and a delegate for the editing?[/quote]
Oooh great Gerolf, I've never thought about it !
Thanks for your answer !
But for a better explanation, the last block is not implemented, I replace the loop :
@/* Loop for Tx Display Area Layout */
for(int indexLayout = 0; indexLayout < TX_DISPLAY_NUMBER; indexLayout++){
tx_ScrollAreaLayout->addWidget(tx_Label[indexLayout] , indexLayout+1 , 0, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_Channel[indexLayout] , indexLayout+1 , 1, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_SDI[indexLayout] , indexLayout+1 , 2, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_Data[indexLayout] , indexLayout+1 , 3, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_SSM[indexLayout] , indexLayout+1 , 4, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_Parity[indexLayout] , indexLayout+1 , 5, Qt::AlignLeft);
tx_ScrollAreaLayout->addWidget(tx_Status[indexLayout] , indexLayout+1 , 6, Qt::AlignLeft);
}@by the loop :
@for(int indexLayout = 0; indexLayout < TX_DISPLAY_NUMBER; indexLayout++){
txDisplayArea->setWidget(tx_Label[indexLayout]);
txDisplayArea->setWidget(tx_Channel[indexLayout]);
txDisplayArea->setWidget(tx_SDI[indexLayout]);
txDisplayArea->setWidget(tx_Data[indexLayout]);
txDisplayArea->setWidget(tx_SSM[indexLayout]);
txDisplayArea->setWidget(tx_Parity[indexLayout]);
txDisplayArea->setWidget(tx_Status[indexLayout]);
}@but each loop doesn't make what I want : set the scroll bar
So I'll try with QTableView, I think this is the best way to realize what I want!
Thanks a lot !