How to hide a widget created by QtDesigner "design" mode programmatically? - qt

I want to know how to create a QLabel in QtCreator Design mode that is hidden by default?In the properties list in the right hand side panel , there's not any option regarding visibility and also adding label->hide() to setupUi function (after compiling ui file and adding to the project headers) makes no difference.

Designer doesn't expose the visible property. You could hand-edit the UI file, but that will be overwritten next time you edit in Designer. It's best to leave it visible in Designer, and just write a line of code after you call setupUi() to hide the widget you don't want to be initially shown.

Related

Expandable "statusbar" in Qt (QtDesigner)?

Basically, what I want to implement is, a sort of an "expandable statusbar" (not sure if there is another name for it); in essence, in "collapsed state", it would be just a plain statusbar, able to show text strings/messages - except it has a "button" for expanding, here a "+":
When the expanding button is clicked, then the statusbar expands into a panel, covering about 1/3rd of the window (however, not "pushing" the preexesting widgets in the window, just covering them) - and the button changes into a "-":
Of course, then I'd want different widgets in the expanded state panel; likewise, if here I click on "-", I go back to the collapsed statusbar state.
I was wandering:
Is there already an existing widget in Qt that would be especially applicable to implementing something like this?
If not, is it possible to somehow implement this widget with existing QtWidgets, such that: at least the collapsed state is shown in QtDesigner UI, - and toggling between collapsed and expanded state is shown in QtDesigner Preview (that is, without a standalone application)?
If previous is not possible - is it possible to code a custom widget (in a form of a .dll, maybe?) that, when imported in QtDesigner, would demonstrate toggling in QtDesigner preview?
Ignoring QtDesigner, what would be the recommended setup to implement something like this purely in code (say, in PyQt5)?
Turns out, there is already a post discussing this - it is here:
How to make an expandable/collapsable section widget in Qt

QDockWidget form in Qt Creator Designer

I'm using Qt Creator 4.5.0 and am trying to create a QDockWidget that I could modify in the builtin designer (I do this all the time with QDialogs and QMainWindows, so this is the first time trying with a QDockWidget). But I'm having no luck with being able to add any widget elements of any kind to the QDockWidget.
Here are the steps I've taken
In the Projects tree on the left, right click on the project and select "Add New..."
In the window that pops up select "Qt" on the left side, then select "Qt Designer Form Class", then select "Choose"
On this page expand the section for "Widgets", then select "QDockWidget", then select "Next"
On this page give the class a name (for me it's "ImageFilesDockWidget"), then click "Next"
On this page select "Finish" to add the files to the project.
From here the "ImageFilesDockWidget.ui" file will automatically show up, so I tried to add some widgets to the view, but nothing would get added. For example, if I clicked and dragged a pushbutton into the center of the dockwidget, then it displayed a red circle with a line through it to indicate I couldn't add the item.
If anyone has run into this problem and knows how to make it work, then that'd be an immense help to me.
Thanks in advance.
update
Currently I'm able to use the designer to customize a standard QWidget object (call it "ImageFilesWidget.ui"). So at the moment my solution is to add a standard QDockWidget to my QMainWindow in the designer, then (still in the designer) I promote the dockWidgetContents from a standard QWidget to my ImageFilesWidget class.
It seems like the problem is when qtcreator 4.5 creates the dockwidgets ui file for you, it doesn't include the "dockWidgetContents" widget that is included in previous versions. Just manually put <widget class="QWidget" name="dockWidgetContents"/> under the "windowTitle" property of the dockwidget and you'll be able to add ui elements to it.

How to correctly set the color of a QPushButton?

I can successfully change the color of a QPushButton using setStyleSheet, but because I'm using QT Creator to make the GUI, every time I run qmake and make, the calls to setStyleSheet disappear.
Changing the palette of the button doesn't change its color either.
What's the best way to change to color of the button without having to manually change my ui_window.h file every time I qmake?
Using style sheets is the right way to do, no matter you're using Qt Creator or not.
From what you are describing, it seems you are writing yourself some code into the ui_window.h, which is the wrong way to set the stylesheet.
You can set it in the constructor of your window class, or set it from the GUI editor (Qt Designer) :
double-click on your window.ui in the project tree displayed by Qt Creator.
select your QPushButton in the GUI editor.
locate the styleSheet property in the properties editor.
click on the three dots (...) : this will bring a stylesheet editor.
If you set the stylesheet with the stylesheet editor, nothing will disappear each time you are rebuilding your app.

Qt can I drag and drop from widget to Qwebkit?

I'm developing simple HTML editor and I like to be able to drag and drop from a button that for example represent HTML text line and in the Qwebkit the HTML text line will be created does Qt support such action?
How should I approach such thing?
I believe it does, yes.
What you need to do is set the mime type of your drag event. See here for details. Then on the webkit side, you can read the drops mime type to see what it was.
You can then try one of the following approaches:
Subclassing QWebView to implement dragEnterEvent and dropEvent. You can use event->pos() in the dropEvent to get the position where the drop occured.
Implementing the drop in javascript within your page, eg setting up an event listener for drops or however its done (I've never tried this).

QStackedWidget inside QTabWidget tab?

In my QT application I use a QTabWidget for the base navigation. This QTabWidget I setup in the ui. In some of the tabs of the QTabWidget I need to have QStackedWidget to be able to "drill down in the view".
I tried adding the QStackedWidget inside the ui also but it automatically adds a page to the stack. I want to add the pages for the QStackedWidget in code instead. If I in the code try to do this the stackedWidget already have a standard page so myWidget will be the second in the stack.
MyWidget *myWidget = new MyWidget(ui.stackedWidget);
ui.stackedWidget->addWidget(myWidget);
What is the best and easiest way to setup a QStackedWidget inside QTabWidget tab?
How about:
QTabWidget *myTabWidget = new QTabWidget(this);
QStackedWidget *myStackedWidget = new QStackedWidget(myTabWidget);
myTabWidget->addTab(myStackedWidget, "Stacked Widget");
Also you can remove all existing stack pages in Qt's Designer/Creator. Just right-click on the stacked widget and remove all existing pages. Then you can add the needed pages in the code using addWidget().
I'd say - create it in ui, just like you do (this way it's easier to layout/position, add other widgets on the tab later, etc), but simply remove all existing pages (added by designer) from code and add your new ones.
Actually Designer from Qt 4.6 allows to delete all pages from stacked widget - you need to right click, go to submenu "Page X of Y", and choose Delete. Repeat until all pages are gone :)
Maybe this got added to the Designer just recently, so you may still need to remove them from the code if you have an earlier version of Qt.
Speaking of keeping stuff inside ui against keeping it in code i'd vote for "as much in UI-file as possible" :)

Resources