Name and implementations of the special "+" tab - qt

I have always been wondering whether the special "+" tab used to add other tabs (as in internet browsers) had a special name.
Also, is there any popular framework (Qt, wxWidgets, etc...) that has a built-in feature for that in its Notebook/TabWidget?
EDIT: Some asked why would such a feature exist while it's simple to implement it. Well, the answer is simple: every widely used feature deserve a dedicated component. It may be simple to implement in a classic tab widget, but it becames harder to do when we can move the tabs (this special one should always be the last) or when it's possible to close the tabs without having to have them selected (in some tab widgets, there's a cross to close the tab on every tab).
Well, considering the different frameworks, it may be simpler or harder to implement. Hence this question to know whether the feature is standard enough to have a dedicated name and some "built-in" implementations in some frameworks :)

As for current versions of WPF, Winform and Qt there is not any tab control with such a feature, but it could be added easily.
check links for similar examples in WPF and Winform.

With Qt you can use code like this:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(changedTab(int)));
}
void MainWindow::changedTab(int tabId) {
if (tabId == ui->tabWidget->count() - 1) {
QWidget *newTab = new QWidget;
ui->tabWidget->insertTab(tabId, newTab, "new label");
ui->tabWidget->setCurrentIndex(tabId);
}
}
For close tab see setTabsClosable()

Related

Qt5 dynamic translation issues for C++ defined properties

I am currently working on somebody else code and I need to fix a bug linked with dynamic translation.
When the language is changed, the Loader is reloaded, it works but it generates unwanted effects (including the bug mentioned above).
So I tried to look for a way to dynamically change the translation without reloading everything.
I added m_engine->retranslate() in my switchLanguage function and this works perfectly, but only for texts directly defined in QML files. The thing is there is also a lot of text defined with setContextProperty in the C++ main controller class, and for them, it doesn't work at all (which seems pretty normal since m_engine is a QQmlApplicationEngine).
I don't see how I can simply force these texts to retranslate too. I have them in pretty much every controller function and they are used by different QML files. I am afraid that there will be no other choice but to change completely the way translation is managed. I hope advanced programmers can help me with this.
Other information:
I work with 5.13.0 version of Qt.
I don't use Designer and cannot use ui.retranslateUi().
It's hard to tell how your main controller class looks like, so here is a short general answer.
You can install an eventFilter and listen for LanguageChange.
In constructor of "main controller class", add this:
auto *core = QCoreApplication::instance();
if(core != nullptr)
{
core->installEventFilter(this);
}
Then add a function to your class:
bool MainControllerClass::eventFilter(QObject *watched, QEvent *event)
{
Q_UNUSED(watched);
if(event->type() == QEvent::LanguageChange)
{
//set properties again or emit property changed signals
}
}

QTabWidget methods not available in QtDesigner

One of the methods available in the QTabWidget class is setTabEnabled - I can't see of a way to set this in QtDesigner (I know I can set it in the code) - is there a specific reason why this method (and presumably others?) aren't settable in QtDesigner?
Just change enabled property of respective widget inside QTabWidget.
according to documentation this suppose to work. I've taken a look on QTabWidget code and it looks like either documentation is wrong or there is a bug in code. I will not explain why just provide workaround on it.
Create this method:
void tabBarWorkaround(QTabWidget *tabWidget) {
for (int i=0; i<tabWidget->count(); ++i) {
tabWidget->setTabEnabled(i, tabWidget->widget(i)->isEnabled());
}
}
And use it in construction time:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tabBarWorkaround(ui->tabWidget);
}
From this moment changing enabled property of page in designer will work like you want.
I've test this with Qt 5.2.1 and it works.
I've realised that there are two separate possible events:
unchecking the 'enabled' checkbox for a tab widget in QtDesigner actually invokes self.<tabname>.setEnabled(False), which seems to disable all child widgets within the tab, but does not disable the tab button itself(!)
Disabling the tab button so that it's not clickable is not possible in QtDesigner, but is possible in code (as discussed in the previous answer): self._uiform.<tab frame>.setTabEnabled(<tab index>,False)

How to access the set qt stylesheet properties (css like grammar), or is there an css to xml converter?

Hy,
i m working on a qt application styled by a stylesheet, set like
QApplication qApplication(argc, argv);
QFile styleFile("myStyleFile.stylesheet");
bool check = styleFile.open(QFile::ReadOnly);
qApplication.setStyleSheet(styleFile.readAll());
the important window of this application uses a QGraphicsScene (which items are not styleable with stylesheets).
But i wannt a unique look.
So the questions are:
1.) Is there a way to access the set stylesheet properties ( like getProperty("QMenu::item:selected") )?
2.) or does anyone know a css-syntax to xml-file tool? (than one could access the set properties with the qt xml/dom support)
I know that some special properties can be accessed like
QColor mainWindowbackgroundColor =
palette().color( QWidget::backgroundRole() );//Get the backgroundcolor set by stylesheet.
but i am searching for a way to access ALL set properties.
Thank you!
I think you will need to use private Qt classes to do this. This is generally not a good idea as the interfaces are internal and subject to change.
In the Qt (4.8.4) sources \src\gui\text\qcssparser_p.h header the QCss namespace is declared.
Whilst I haven't tried this, it looks like you will need to create a QCss::Parser, call parse to get a QCss::StyleSheet. This object contains the parsed data including a vector of QCss::StyleRule which matches QCss::Selector and QCss::Declaration together, have a look at the comment above the QCss::Declaration to see how it is all broken down.
Final Warning: Using Qt private interfaces is liable to cause maintenance problems - don't do it without a very good reason.

How can I change the side on which a Qt::Drawer opens on Mac?

Greetings!
I have a QDialog myDialog; and want to attach additional information to it using QWidget myDrawer( &myDialog, Qt::Drawer ). This works fine, except that the drawer always attaches itself to the left side of myDialog. I need it on the right side. And I have no clue how to do that.
Any hints and ideas would be greatly appreciated!
Thanks, and best regards,
Robin
This is normally done by the OS. Try moving the window close to the left edge of your screen and open the drawer - it should now open on the right-hand side.
Generally, try to avoid using drawers, as they are seemingly being frowned upon and not part of the expected "look and feel" these days anymore.
Update:
Sorry - Correct, they are still in the Human Interace Guidelines, but most developers don't use them anymore. From discussions on several boards and IRC channels it seems that they feel "unnatural" as they are not part of the "window".
To get back to your question; I doubt you can control the side the drawer slides out from Qt. Plus it might make a difference depending on if you are using Cocoa- or Carbon-Qt. At least I couldn't find anything regarding that in the Qt documentation.
In the demos\mainwindow project, there are a few lines (lines 311-314) in mainwindow.cpp:
#ifndef Q_WS_MAC
{ "Black", 0, Qt::LeftDockWidgetArea },
#else
{ "Black", Qt::Drawer, Qt::LeftDockWidgetArea }
I don't have access to OS X right now, but I remember I learnt to change the side of the drawer by the above-mentioned project. So you could playaround with this project in OS X.
this is possible:
but you will have to use QDockWidget as the Qt::Drawer widget;
the following code is from a QMainWindow class:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDockWidget *drawdock = new QDockWidget(this,Qt::Drawer);
this->addDockWidget(Qt::RightDockWidgetArea,drawdock);
}
To change the side of the drawer during runtime:
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(this);
mainWindow->addDockWidget(Qt::BottomDockWidgetArea,drawdock);
The areas could be any one of the DockWidgetAreas

How to know that widget is currently is running in Qt Designer

How can I in code of the custom Qt widget know that it is currently instantiated in Qt designer?
Use case:
I build a complex custom widget that has several child widgets like QPushButton, QLabel etc.
As application logic require, when widget is created most of those sub component are not visible but in design time when I put it on a form I would like to see them.
To be able to play with style sheet at design time.
Currently what I get is a empty is only a result of constructor - minimal view (actually empty in my case).
What I am looking for is to be able to do something like
MyQWidget::(QWidget *parent)
{
....
if(isRunningInDesigner())
{
myChildWidget1->setVisible(true);
myChildWidget2->setVisible(true);
myChildWidget3->setVisible(true);
}
else
{
myChildWidget1->setVisible(false);
myChildWidget2->setVisible(false);
myChildWidget3->setVisible(false);
}
....
}
So what should I put in to this bool isRunningInDesigner() ?
From the Qt Designer manual:
To give custom widgets special behavior in Qt Designer, provide an implementation of the initialize() function to configure the widget construction process for Qt Designer specific behavior. This function will be called for the first time before any calls to createWidget() and could perhaps set an internal flag that can be tested later when Qt Designer calls the plugin’s createWidget() function.
Those are methods from the QDesignerCustomWidgetInterface plugin interface. In short: you tell the widget to behave differently when Qt Designer asks your plugin to create instances of your custom widget.

Resources