Overlapping widgets in QtDesigner

后端 未结 2 2006
-上瘾入骨i
-上瘾入骨i 2020-12-19 03:26

I want to overlay two widgets in QtDesigner: There is the big QTextBrowser, and below in the down right corner should be a non-interactiv label that I am going to use as a d

相关标签:
2条回答
  • 2020-12-19 03:44

    I had the same problem but I did not manage to add Overlapping widgets in QtDesigner. Instead, I had to create the overlapping one dynamically after initializing my MainWindow.

    I've got two widgets:

    • dataset_tableWidget (tableWidget)
    • spinner_dataset_tableWidget (QtWaitingSpinner)

    and I wanted to make spinner_dataset_tableWidget spin over the dataset_tableWidget.

    After initializing the MainWindow you can do:

    #Crating QtWaitingSpinners dinamically and positioning it over the tableWidgets
    dataset_tableWidget = QtWaitingSpinner(dataset_tableWidget)
    dataset_tableWidget.setSizePolicy(dataset_tableWidget.sizePolicy())
    
    0 讨论(0)
  • 2020-12-19 03:57

    This is usually simplest to achieve by using QGridLayout. It allows widgets to occupy the same grid cells. For this particular problem, a 1x1 grid is enough.

    Steps to try it out with designer:

    1. Create new form, plain Widget for simplicity
    2. Add a text edit to it (drag and drop from Widget Box), and from Object Inspector you should see it becomes child of the root widget
    3. Add a label to it (drag and drop from Widget Box), and from Object Inspector you should see it becomes child of the root widget
    4. Right click on the root widget (easiest in the Object Inspector), and from the bottom of context menu, select Lay out > - Lay out in Grid
    5. Right click on the label, and from Layout alignment > set it aligned to the corner you want

    Done. Here's what it looks like in my Designer:

    enter image description here

    Now adapt above to your real form.


    Ok, it appears achieving above with Designer is hard, and perhaps a bit a matter of luck of doing things just right... Designer just doesn't support doing what you want, it seems.

    For clarity this is a complete source code:

    QGridLayout *layout = new QGridLayout(widget);
    QTextBrowser *textBrowser = new QTextBrowser();
    QLabel *label = new QLabel();
    label->setText("Overlaid Text");
    
    //label gets positioned above textBrowser and is an overlay
    layout->addWidget(textBrowser, 0, 0, Qt::AlignLeft | Qt::AlignTop);
    layout->addWidget(label, 0, 0, Qt::AlignRight | Qt::AlignBottom); 
    
    0 讨论(0)
提交回复
热议问题