How to put QPushButton into Qt3DWindow which is in the widget? - c++

I want to have one or more QPushButtons or other widgets on top of Qt3DWidget, but it is invisible. Don't know why.
In my mainwindow.cpp I have such code:
this->renderer = new Qt3DExtras::Qt3DWindow();
this->renderer->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d41));
this->rendererContainer = QWidget::createWindowContainer(this->renderer);
ui->centralWidget->layout()->addWidget(this->rendererContainer);
this->shotButton = new QPushButton("Make photo", this->rendererContainer);
this->shotButton->move(this->rendererContainer->width() / 2 - (this->shotButton->width()-20) / 2, this->rendererContainer->height()-40);
It worked when I tried to use QOpenGLWidget, but with Qt3DWindow this button is always invisible. Looks like Qt3DWindow draws on top of it.

I found the reason, but I can't say that I found the solution. The widget, provided by 'createWindowContainer', is drawing on top of everything of the parent window, according to docs. Every single widget in the same position with it will be under it and invisible for user.
The only way which I found to put something on top of Qt3D is to write an app by using QtQuick and QScene3D.

Related

Qt: How do I stack widgets to do a transition between them, by fading one in and the other out?

Here's my end goal: I want to transition between two images. I want the original image to fade out, and at the same time, the new image to fade in.
I know I need to modify the opacity, and I found this great link for "How to make Qt widgets fade in or out": How to make Qt widgets fade in or fade out?
I also know I need to set the image for the QWidget, and I found out how to do that with the following code:
m_image1 = new QWidget();
m_image1->setStyleSheet("border-image: url(\"" + m_imagePath1 + "\") 0 0 0 0 strech strech; ");
Where I'm stuck is the layout. How do I lay a QWidget directly on top of another QWidget.
I did read about QStackedWidget, and it seems like it would be useful: https://doc.qt.io/qt-5/qstackedwidget.html . There's a method to set the current widget via setCurrentIndex(int). However, it doesn't seem to allow me to be transitioning out a widget and transitioning in a widget at the same time. It just either is one widget or the other widget. And from the class description, it says "a stack of widgets where only one widget is visible at a time."
A lot of the layouts I'm aware of, position items next to each other, not on top.
So, my question is how do I stack the widgets to do a transition between the widget contents, by fading them in/out?
Thank you.

Modifying the QDockWidget default snap locations?

I am trying to model a UI from a wxWidgets application to QT. However, in the original application they have Dock Widgets that can snap in place and move around except for occupying the main window.
Every box appears to be a dock widget except for the main window. And if I want to move the Datasets window (top left) to the right of the Output Adjustment window I can:
So now my goal was to try and get the UI in QT to at least accomplish the dock widgets on the left side of the main widget. Which in my case, I was able to at least get the width and the height with some trouble to show up the same. However, I cannot create a QDockWidget like the Output Adjustment and have it snap next to the others. And the other problem comes when I try to move a dock widget it will not snap to the right or the left of the other widgets.
This is where I have my problems. I cannot snap it to either side of the dock widgets. Is this something that will need to be hard coded? I know I have seen other examples online that look awfully similar to this layout. However they are all hard coded. And it would be a lot more simple if I could just use the creator to add the sliders and toolbars as time goes on. And if it is not something I have to hard code, how do I modify the locations on where my widgets can snap? I tried to make a grid layout, however the widgets do not go into the grid.
By enabling dock nesting in your main window you can achieve a more flexible placement of the dock widgets, including positioning the dock widgets side-by-side. In Qt Designer, select the main window and in the Property Editor panel mark the dockNestingEnabled check box (present in the QMainWindow section). Alternatively, you can achieve the same result by calling the method QMainWindow::setDockNestingEnabled in your code.
If you want your application to start with dock widgets arranged side-by-side as in your reference application, you have to resort on the method QMainWindow::splitDockWidget. For example, you can create an initial arrangement in Qt Designer such as the one depicted below.
Next, you can rearrange the dock widgets in the main window constructor code,
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
// Changes the dock widgets arrangement.
splitDockWidget(ui->dock1, ui->dock3, Qt::Orientation::Horizontal);
splitDockWidget(ui->dock1, ui->dock2, Qt::Orientation::Vertical);
}
which would produce the result below.

In QT "ui->scrollArea->horizontalScrollBar()->setValue(0)" has no effect

So in my UI designer I have a ScrollArea Widget, I then in my MainWindow.cpp, I create a QGraphicScene and a QGraphics View. I create a new widget and give that widget a QVBoxLayout so that it will auto-size(which is correct to my understanding).
I then use ui->scrollArea->setWidget(widget); to make this new widget the child of my scrollView.
All of this seems correct because I have scroll bars that I can navigate my scene with. However; using the line ui->scrollArea->horizontalScrollBar()->setValue(0); still has no effect on the scroll bar values.
scene = new QGraphicsScene();
scene->setSceneRect(0,0,2500,2500);
view = new QGraphicsView(scene);
QWidget *widget = new QWidget;
view->setBackgroundBrush(Qt::white);
QVBoxLayout* bLayout = new QVBoxLayout(widget);
ui->scrollArea->setWidget(widget);
bLayout->addWidget(view);
widget->show();
ui->scrollArea->horizontalScrollBar()->setValue(0);
ui->scrollArea->verticalScrollBar()->setValue(0);
I just had the this problem. Then, after debugging with ui->scrollArea->verticalScrollBar()->value() I realized that the scrolling area has no size before the component is show on the screen, i.e., it does not change the scrolling because it is not visible yet.
This is a sample Python code, but the is the same for C++, except the Language Syntax.
from PyQt5.QtWidgets import QDialog
...
dialog = QDialog()
...
verticalScrollBar = dialog.QPlainTextEdit.verticalScrollBar()
horizontalScrollBar = dialog.QPlainTextEdit.horizontalScrollBar()
# Has no effect, if you print the values, you will see always 0
verticalScrollBar.setValue( horizontalScrollBar.maximum() )
horizontalScrollBar.setValue( horizontalScrollBar.minimum() )
dialog.show()
# Now it has effect
verticalScrollBar.setValue( horizontalScrollBar.maximum() )
horizontalScrollBar.setValue( horizontalScrollBar.minimum() )
Autoscroll PyQT QTextWidget
If you are sure to address the correct scrollbar (as pointed in the comments by thuga), maybe check if your scroll area is modified after that init. I mean I'm not sure of the bahaviour, but if you modified some text in your widget for example, I think the scrollbar will be impacted.
You may need to catch some of your widget's event to reapply those:
ui->scrollArea->horizontalScrollBar()->setValue(0);
ui->scrollArea->verticalScrollBar()->setValue(0);
If it doesn't help, you should try to debug by tracking scrollbar value with
ui->scrollArea->verticalScrollBar()->value()
and see if your set is well applied, and maybe when it is overriden
Just in case, you may also try some of the methods indicated here, but it's probably not relevant to your issue: setValue of ScrollBar Don't work at first time

How to manage QSplitter in Qt Designer

When I press a button, I bring up a dialog where user select things and press 'Ok' at the end. I want a splitter in this dialog. Left pane will show tree and right will show something else. How do I do that right?
From Qt example itself:
QSplitter *splitter = new QSplitter(parent);
QListView *listview = new QListView;
QTreeView *treeview = new QTreeView;
QTextEdit *textedit = new QTextEdit;
splitter->addWidget(listview);
splitter->addWidget(treeview);
splitter->addWidget(textedit);
So in this example, splitter is created without any dialog resource. If I have to create this way, that would mean I have to create all my controls in the code as well rather than Qt Creator.
What is the right way to do this when I need other controls on the screen?
You can simply create splitter containing items in Qt Designer :
First place your widgets on your dialog or widget in designer (They should not be in a layout)
Select the widgets that you want to be in a splitter (By holding CTL and clicking on them)
Right click on a selected widget and from Layout menu select Lay Out Horizontally in Splitter or Lay Out Vertically in Splitter.
Now apply a grid layout to the dialog and everything should be OK. You would see something like this in Object Inspector View :
Okay, I know this is ancient, but here's the complete answer.
First, within some sort of widget container, plop your pieces in. For the window I just did, I have a Widget as my window. I put two widgets inside that labeled something like topContainer and bottomContainer. I then put all the widgets they each need into them, and gave them their own layouts.
Then do NOT select the main container. Select the two widgets you want to split. You're in effect putting a splitter on them, not on the main container. So I went to the widget list window and selected both together, then right-click for the dialog window, scroll down to the Layout option, and "Lay Out Vertically in a Splitter" is NOT greyed out. Select it.
You still need a layout on the main container. A splitter is not a layout. So at that point, I just put a vertical layout on the main container.
To repeat: you are NOT setting a layout on the container holding the pieces you're trying to split. You are selecting the two widgets to split and adding a QSplitter around them. That's the trick to get it to work.
You can still create your controls in a .ui file using Qt Designer (integrated in Qt Creator). Within Qt Designer, add a QWidget object to your dialog. Then, from QDialog derived class you'll write, directly in your constructor, create your QSplitter using the QWidget object as a parent.
This way, you can create all but the splitter object from Qt Designer.
I think it's also possible to create the QSplitter (as you can create a QButton, QCheckBox...) item directly from Qt Designer.

White screen observed while launching the QGraphicsView application

Our application uses Qt's Graphics View framework to load the html pages. QGraphicsWebView loads local html page which is black background. But always observed the white screen while launching the application. I have tried setting black background for both QGraphicsView and QGraphicsScene. Nothing worked for me.
Here's the sample code for your reference.
MainWindow which inherited from QMainWindow class
mGraphicsScene = new QGraphicsScene(this);
mGraphicsView = new QGraphicsView(mGraphicsScene);
mGraphicsView->setViewport(new QGLWidget(this));
mGraphicsWebView = new QGraphicsWebView;
mGraphicsWebView->setUrl(QUrl("https://www.google.co.in/"));
mGraphicsScene->addItem(mGraphicsWebView);
setCentralWidget(mGraphicsView);
Is there any way to avoid white screen of the application?
Best Regards,
Pratap
Try next. Why did you see white? Because item already added, but page not loaded, so you see white(blank) item without page. Set to your scene some black pixmap, connect loadFinished signal to special slot, where you add item to your scene. In this case scene will be black, but when page will be loaded, your slot will add this on scnen and you will see only page.
mGraphicsScene = new QGraphicsScene(this);
mGraphicsScene->addItem(new QGraphicsPixmapItem(QPixmap("G:/2/qt.jpg")));
mGraphicsView = new QGraphicsView(mGraphicsScene);
mGraphicsView->setViewport(new QGLWidget(this));
mGraphicsWebView = new QGraphicsWebView;
mGraphicsWebView->setUrl(QUrl("https://www.google.co.in/"));
connect(mGraphicsWebView,SIGNAL(loadFinished(bool)),this,SLOT(slotLoaded()));
//mGraphicsScene->addItem(mGraphicsWebView);
mGraphicsView->resize(1000,700);
mGraphicsView->show();
Slot:
void MainWindow::slotLoaded()
{
mGraphicsScene->addItem(mGraphicsWebView);
}
For example black pixmap which was created by code:
QPixmap black(1000,700);
black.fill(Qt::black);
mGraphicsScene = new QGraphicsScene(this);
mGraphicsScene->addItem(new QGraphicsPixmapItem(black));
When application start:
As you can see, all is black, when page was loaded:
As you can see, it is normal page. It is not very beautiful because I use fast settings and resize window and so on, but you set graphicsview as central widget, do it will be more beautiful.
Thank you very much for the response.
I have tried your solution and also observed white screen while launching the application on Windows
I found the culprit is mGraphicsWebView->setUrl(QUrl("https://www.google.co.in/")); This is blocking all other widgets on the scene. So I have added a singleShot timer and kept this statement under that.
//QTimer::singleShot(100, this, SLOT(loadUrl())); Then it works fine.
Please let me know if you have any better idea.