Set alignment of QGroupBox title - c++

I try programmatically set alignment of title of QGroupBox. According to the documentation I try to do it in the following way:
MainWidget::MainWidget (QWidget * parent)
: QWidget (parent)
{
setWindowTitle (tr ("QGropBox Title Alignment issue") );
QGroupBox * group = new QGroupBox ("Group Title", this);
QVBoxLayout * layoutTop = new QVBoxLayout ();
layoutTop->addWidget (group);
group->setAlignment (Qt::AlignHCenter);
this->setLayout (layoutTop);
}
But I have got unexpected result and title aligned with the left-hand side of the group box.
Such behaviour I have got with the environment:
Debian 9.0 (sid);
Qt 5.5.1 (installed from repository);
XMonad DE.
The same behaviour I have got with the next environment:
Ubuntu 15.10;
Qt 5.4 (installed manually using offline-installer);
KDE.
But! If I use Qt installed from repository (Qt 5.4.2) the title will aligned with the centre by default. But if I change align to the left it will not changed.
I try find some solution with Google, but I haven't found similar questions and this is bad sign and usually this is means that I do some thing wrong.
So my question is: Why I can't manage by alignment of the group box title.

It seems like in Qt 5.5.1 there is a bug for some visual styles when QGroupBox title alignment is not taken into account when title's rect is computed: QTBUG-49068: QGroupBox title does not follow alignment with fusion style.
You can download most recent version from git repository, build it and check if the bug has been fixed (is has been for Fusion style: qt commit 139953).
If bug still occurs, I think you should submit an issue to Qt bugtracker.

Related

Qt - Change application title dynamically on macOS [duplicate]

This question already has an answer here:
How to rename the program-name menu-label in a MacOS/X program?
(1 answer)
Closed 1 year ago.
I want to change the title of my Qt 5.15.0 application after it was executed.
For doing that, I'm using the setWindowTitle function of the QWidget class, like so:
setWindowTitle("My new window title");
Turns out the title is changed correctly on Windows but not on macOS. Here are the results:
Windows 10
macOS Catalina
You can see that in macOS the title is updated in the Window but not in the MenuBar (top) and in the Dock (bottom).
Is there a way to change all the titles on macOS once the app is running?
Edit
I managed to change the MenuBar title using the Objective-C code posted in this question (I just had to rename my .cpp file to .mm):
#import <AppKit/NSApplication.h>
#import <AppKit/NSMenu.h>
void setMenuProgramName(const char* newName)
{
NSMenu* mainMenu = [[NSApplication sharedApplication] mainMenu];
NSMenu* appMenu = [[mainMenu itemAtIndex:0] submenu];
[appMenu setTitle:[[NSString alloc] initWithUTF8String:newName]];
}
There's still the problem with changing the name in the Dock. There are some insights here and here but I couldn't make it work yet. It is said that I should "comment out NSApplicationMain in supporting files -> main.m" but I don't have that file and I don't know how could I do that using Qt. That's why I propose that the question be reopened since there's no solution for that yet.
Edit II
Apparently, as mentioned in this response, there's no way to change the title when you hover over the app icon in the Dock.
Alternatively, you can add a "badge" with the following code, but that's meant for notifications.
[[NSApp dockTile] setBadgeLabel:#"My custom text"]
It would be nice to know why this can't be changed - if it's because of a bad design decision or just a poorly developed framework.
you can like this:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QString windowTitle("New Title");
this->setWindowTitle(windowTitle);
}

QtVirtualKeyboard focus problem when editing a cell in a QTableView

I have been battling for the last couple of days to include the QtVirtualKeyboard into my QWidget based app that is running on a Raspberry Pi with a 7" touch screen display.
Here's what I've done so far :
Installed the plugin :
sudo apt-get install -y qtvirtualkeyboard-plugin
sudo apt-get install -y qml-module-qtquick-controls2
sudo apt-get install -y qtdeclarative5-dev
sudo apt-get install qml-module-qt-labs-folderlistmodel
Added the QT_IM_MODULE environment variable and set that to qtvirtualkeyboard
Added QT += quickwidgets to my .pro
Created a QQuickWidget to place my virtual keyboard.
.h
private:
QQuickWidget *m_quickWidget;
.cpp
// In constructor
QUrl source(QML_FILE_PATH + "virtualkeyboard.qml");
m_quickWidget->setSource(source);
m_quickWidget->setAttribute(Qt::WA_AcceptTouchEvents);
ui->verticalLayout->addWidget(m_quickWidget);
And finally my virtualkeyboard.qml file
import QtQuick 2.7
import QtQuick.VirtualKeyboard 2.1
Rectangle {
id: window
width: 600
height: 0
InputPanel {
id: inputPanel
width: window.width
states: State {
name: "visible"
when: inputPanel.active
PropertyChanges {
target: window
height: inputPanel.height
}
}
transitions: Transition {
from: ""
to: "visible"
reversible: true
ParallelAnimation {
NumberAnimation {
properties: "y"
duration: 250
easing.type: Easing.InOutQuad
}
}
}
}
}
So up to now, visually everything is looking pretty good. When I open my app, the keyboard widget is not visible (window height: 0 in qml), and when I double-click a cell in my QTableView (which has flags Qt::ItemIsEnabled | Qt::ItemIsEditable), the keyboard widget shows up at the bottom of my vertical layout at the right position and size.
And now to my problems :
The main problem I have is when I double-click on my editable cell, my keyboard widget shows up and my cell seems to still have focus (blinking cursor is visible in the clicked cell). Up to now all is working well. But when I click a button on the virtual keyboard, the editable cell loses focus, the keyboard widget closes, and I get this error in my application console :
InputContext::sendKeyClick(): no focus to send key click - QGuiApplication::focusWindow() is: QWidgetWindow(0x1e68250, name="ConfigWindow") where ConfigWindow is the name of the base widget in my Designer form.
Another smaller yet seemingly unsolvable issue I have is that the keyboard only opens up when I double-click a cell in my QTableView. This is a weird one because i set the editTriggers to CurrentChanged in my designer. I know that works because if I single click my cell, the cursor starts blinking and if I use my physical keyboard which is connected to my raspberry, I can edit the text. (Of course, the physical keyboard is only available during the development of my app and won't be available in the finished product).
I hope I have been clear enough, but will gladly provide more details if required.
Any help on either of these issues would be greatly appreciated.
Cheers.
EDIT:
Some useful links I have come across:
Qt Virtual Keyboard in QQuickWidget
Resize qtvirtualkeyboard according to QObject
Qt Virtual Keyboard
Ok, so after another few days of all out war with the virtual keyboard, I have finally achieved the desired result.
After finding this gem of a guide, it turns out that because the widget containing my QTableView and QtVirtualKeyboard was a QDialog that was being displayed using the exec() method, it meant that the window properties wouldn't allow the keyboard to modify my data. And while the solution proposed in the guide didn't resolve my problem, making my widget inherit QWidget did set me off along the path to getting it all working properly.
I say this because once I changed my QDialog into a QWidget, I then had a console output error saying unknown:0 input method is not set every time I pressed a key.
The solution to this was to remove the Qt:Dialog flag from my setWindowFlags() method. And maybe most importantly, set the focus policy of my QQuickWidget to NoFocus like so:
// In constructor
QUrl source(QML_FILE_PATH + "virtualkeyboard.qml");
m_quickWidget->setSource(source);
m_quickWidget->setAttribute(Qt::WA_AcceptTouchEvents);
m_quickWidget->setFocusPolicy(Qt::NoFocus);
ui->verticalLayout->addWidget(m_quickWidget);
Hallelujah!! My QtVirtualKeyboard finally sends the clicked keys to my editable cell.
Finally, to open the keyboard with a single click on a cell in my table, I'm sure there is a better solution than this, but i connected a slot to my QTableView's pressed signal and manually set the visibility of the input method:
void ConfigWindow::on_tableView_pressed(const QModelIndex &index)
{
if ((index.column() == 0) || (index.column() == 1))
{
QApplication::inputMethod()->show();
}
}
Hope this helps anyone having the same trouble as me with this powerful yet painfully under-documented plugin.

Clicking QSystemTrayIcon brings up an empty context menu on Mate desktop

I'd like to create a tray icon for my application for showing the main window on clicking on it after the former was minimized.
Here's the implementation:
TrayIcon.h:
class TrayIcon_t : public QSystemTrayIcon {
Q_OBJECT
public:
TrayIcon_t();
};
TrayIcon.cpp:
TrayIcon_t::TrayIcon_t() {
setIcon(QIcon(":/icons/tray.ico"));
}
Main.cpp (part only, there's no more code related to the tray):
TrayIcon_t *tray = new TrayIcon_t;
QObject::connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), &MainWindow, SLOT(show()));
tray->show();
While on LXDE it works just fine, on MATE desktop it mostly opens a small menu (I think) containing no items and doesn't emit the signal required by the main window to be shown. See this picture.
Do you think I've encountered a bug in Qt 5.7.0?
Turns out that this is a bug. Reported it and became accepted at:
https://bugreports.qt.io/browse/QTBUG-55717

Transparent Background in QWebEnginePage

We are trying to port some application from Qt 4 to Qt 5.4. The Qt 5.4 has a new web engine. We used to make the background of QWebView and QWebPage to be transparent:
view = new QWebView(this);
QPalette palette = view->palette();
palette.setBrush(QPalette::Base, Qt::transparent);
view->page()->setPalette(palette);
view->setAttribute(Qt::WA_OpaquePaintEvent, false);
But this code doesn't work for QWebEngineView and QWebEnginePage. The point is that QWebEnginePage doesn't have such an API like setPalette.
Can anyone find a way to solve this?
As mentioned in https://bugreports.qt.io/browse/QTBUG-41960, this is now working by simply using this line:
webEngineView->page()->setBackgroundColor(Qt::transparent);
I've tried it in Qt 5.6 and it works well.
Update: In order to make this answer more helpful, let me show all relevant code.
In MainWindow, I have set this:
setAttribute(Qt::WA_TranslucentBackground);
setAutoFillBackground(true);
setWindowFlags(Qt::FramelessWindowHint);
For the webEngineView object, I have set these attributes:
webEngineView->setAttribute(Qt::WA_TranslucentBackground);
webEngineView->setStyleSheet("background:transparent");
webEnginePage = webEngineView->page();
// https://bugreports.qt.io/browse/QTBUG-41960
webEnginePage->setBackgroundColor(Qt::transparent);
No. A partial solution has been committed to upstream, but it covers only QtQuick and you can't have any elements on top:
https://bugreports.qt.io/browse/QTBUG-41960

How to gray out a menu item again in Qt

I am working with qt and have created a menu bar like "File" with some sub menu items "Open", "Save", "SaveAs", "Close", "Quit". I also created actions like "actionNew", "actionOpen", ... and so on. I used the same actions for the tool bar and disable the menu and tool bar items into the constructor with the "disableItems()" function, that works fine and the tool bar and menu items are gray out. If I clicked the sub menu item "New", the tool bar and menu items are enabled and not gray out, that also works fine, but if I clicked the sub menu item "Close" only the tool bar items gray out and the menu items are still enabled :(. How can i fix it? Hope you can help me and sorry for my bad English ;)
MainWindow::MainWindow(QWidget *parent)
:QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* disable menu items and tool bar items */
disableItems();
}
void MainWindow::disableItems()
{
ui->actionSave->setEnabled(false);
ui->actionSaveAs->setEnabled(false);
ui->actionClose->setEnabled(false);
}
void MainWindow::enableItems()
{
ui->actionSave->setEnabled(true);
ui->actionSaveAs->setEnabled(true);
ui->actionClose->setEnabled(true);
}
void MainWindow::on_actionNew_triggered()
{
enableItems();
}
void MainWindow::on_actionClose_triggered()
{
disableItems();
}
I had the same problem.
I was puzzled because the code behaved differently from Linux 64 (where usually I develop), Windows (customer machine), Linux 32 (old development & backup machine). Then I realized the difference in version.
The only way to solve I found was to upgrade from default QtSDK that came with my Ubuntu distribution to the latest downloaded from here.
I suggest to check if the version you are using can be upgraded.
HTH
edit I noticed that they changed something more radical: after the upgrade the menubar is not more shared on 'top screen' but more traditionally inside the 'main window'. Probably the team overlooked a portability problem, that's reasonable considering the wide target they have.