Quick question about using the QTabWidget 'addTab' function - c++

Hopefully a very quick question. In one of my functions I want to generate an 'x' number of tabs for a QTabWidget during run-time ('x' provided by user). I know I have to use the addTab function for the QTabWidget (correct me if I'm wrong), but I am unsure as to how I am supposed to use it. The qt documentation was unclear to me.
I have tried the following command to add only 1 tab as a test, but it caused the program to crash:
ui->checkBoxTabArea->addTab(ui->checkBoxTabArea,"tab2");
Since I am obviously wrong, can somebody help me use this function? I feel like I am just passing the wrong arguments to addTab.
Edit: checkBoxTabArea already has 1 tab when the program starts up (if this is any help).

If you take a look at the documentation, the addTab function says this :
int QTabWidget::addTab ( QWidget * page, const QString & label )
Adds a tab with the given page and label to the tab widget, and
returns the index of the tab in the tab bar.
So the argument you pass should not be the TabWidget you want to add the tab to, rather it should be the widget you want to add as the tab.
What you should do is something like :
QLabel *myLabel = new QLabel("Hello World", this);
ui->checkBoxTabArea->addTab(myLabel, "My Label Tab");
This will add a single tab to the tab widget, which contains myLabel.

Related

Changing qt5 tab names dynamically

Say I have a tabwidget in my ui file
this is how im adding tabs right now:
QPlainTextEdit *tab = new QPlaintextEdit;
int index = ui->tabWidget->addTab(tab, "changeme");
Now I'm wondering if it's possible to change the name of the tab on the go,
for example when subclassing QPLainTextEdit in a class and connecting a signal to it when the text changes then i'd like to add a little star to the tab to indicate that the file has been modified, is it even possible?
QTabWidet::setTabText does what you want.
E.g:
ui->tabWidget->setTabText(index, "new text");

Disable file name box in QFileDialog

I used a QFileDialog to open a browser.
Here is my code:
QString filePath = QFileDialog::getSaveFileName(this,
"Export Xml", "PluginPythonQt",
"Xml files (*.xml)");
When excute it will show a dialog like this:
I want to disable the "File name:" box in the picture or prevent user to enter a new name. How can i do that ? Thanks.
I believe you can't achieve this — save dialog is about choosing name besides the choosing where to save it. Of course, you might just ignore what user typed and force your name when he hits OK but it will just make the user angry.
Better way, in my opinion, is to use QFileDialog::getExistingDirectory which will allow the user to choose where to save the file but won't allow him to choose the file name. It will be fair, at least.
Similar question was answered in https://forum.qt.io/topic/73973/qfiledialog-with-no-edit-box.
In general, you can hide any element in any widget if you dig a bit into widget's source code to find element's name, when you have a name, you can find the corresponding element via findChild<QWidget *>(elementName).
Usually if you check QSomeWidget.h (Qt is open source!) you can find element names very easily as they are typically listed as the widgets members.
To hide both labels, fileEdit, ComboBox and even buttons, you can use this code:
QFileDialog fileDialog = new QFileDialog;
QWidget * fileNameEdit = fileDialog->findChild<QWidget *>("fileNameEdit");
Q_ASSERT(fileNameEdit);
fileNameEdit->setVisible(false);
QWidget * fileNameLabel = fileDialog->findChild<QWidget *>("fileNameLabel");
fileNameLabel->setVisible(false);
QWidget * fileTypeCombo = fileDialog->findChild<QWidget *>("fileTypeCombo");
Q_ASSERT(fileTypeCombo);
fileTypeCombo->setVisible(false);
QWidget * fileTypeLabel = fileDialog->findChild<QWidget *>("fileTypeLabel");
fileTypeLabel->setVisible(false);
QWidget * fileButtonBox = fileDialog->findChild<QWidget *>("buttonBox");
fileButtonBox->setVisible(false);
Note that even though buttons are hidden, typing Enter on keyboard (or double clicking) would trigger Open button, and dialog might disappear if you haven't done anything in Accept method. So it would also be a good idea to handle state of that button as well if you really wish buttons to be hidden as well.

Qt: How to change QWizard default buttons

I'm making a program with Qt 4.8.5 on a Fedora system (unix). It's a QWizard structure with its QWizardPages.
I needed to change the default QWizard buttons (back, next, finish...) and customize it. I found I could do it putting the next lines on the constructor of my QWizard class called BaseWizard (class BaseWizard : public QWizard)
QList<QWizard::WizardButton> button_layout;
button_layout <<QWizard::Stretch << QWizard::CustomButton1 << QWizard::BackButton << QWizard::NextButton << QWizard::FinishButton;
this->setOptions(QWizard::NoDefaultButton);
With that what I have from left to right is one custom buttons, the back button, the next button and the finish button, and when I want I can show or hide the custom buttons with SetVisible() / SetDisabled() / setEnabled() functions.
That worked perfect for what I wanted until now... I have to make some changes to the program so I need to change those buttons depending on the page the user is. As I said before, I know I can change the visibility of CustomButton 1 for example BUT I can't do the same with back button so... my quesiton is: How can I decide which buttons I show in every QWizardPage (and their text) and which is the best way to do it?
I've tried creating a function on my BaseWizard
// Function to have only 2 custom buttons
void BaseWizard::ChangeButtons()
{
QList<QWizard::WizardButton> button_layout;
button_layout <<QWizard::Stretch << QWizard::CustomButton1 << QWizard::CustomButton2;
setButtonLayout(button_layout);
}
And then in the QWizardPage (lets call it WP) using it like:
BaseWizard *bz;
bz->ChangeButtons();
But when I do that nothing changes.I can still see the NextButon for example. I have tried also using first a button_layout.clear(); to see if clenaing it before adding the buttons works, but not.
I have also tried changing the text of CustomButton1. If I do it in the WP after calling ChangeButtons with
wizard()->button(QWizard::CustomButton1)->setText("TEXT CHANGED");
Then the text changes but if I put it in the ChangeButton() function with this->button(QWizard::CustomButton1)->setText("BBBBB"); it does nothing (But its entering into the funcion). Ofc if I try to change the text of CustomButton2 in WP nothing happens because I can't still see that button... so any idea of what I am doing wrong or how could I get what I try will be very apreciated,
Thank you so much.
Ok, I finally knew why was my program crashing... I put here the solution if anyone needs it in the future:
BaseWizard *bz; // <-- HERE is the problem
bz->ChangeButtons();
I was not initializing the pointer so it has to be changed to:
BaseWizard *bz = dynamic_cast<BaseWizard*>(wizard());

QTabWidget I'd like to hid sub-tabs

I have a QTabwidget and 4 QWidget sub-tabs and I'd like to hide 3 sub tabs when I am not using them.
With a 'Enable' button, I want the hidden sub-tabs to appear again.
In order to hide them, I tried removeTab function as below
ui->tabWidget_2->removeTab(3);
ui->tabWidget_2->removeTab(2);
ui->tabWidget_2->removeTab(1);
But then, I don't know how to reinsert the hidden tabs cuz I do not have the pointer to the hidden tabs.
Or is there any other good method to hide them other than removeTab? Please let me know. It'd be really appreciated. Thanks.
You need store "copy" of your tab somewhere and insert this tab again. For example:
QMap<int,QPair<QWidget*,QString> > map;
map.insert(0,QPair<QWidget*,QString>(ui->tabWidget->widget(0),ui->tabWidget->tabText(0)));
//store index, widget and title of tab
ui->tabWidget->removeTab(0);
ui->tabWidget->insertTab(0,map.value(0).first,map.value(0).second);
//restore data
I can't tell you that it is the best approach, but removeTab removes tab but not your widget. So when I used this code(with QTextEdit as widget inside tab for example) and type some words, my tab was successfully restored and I didn't lose my data. If you use QIcon than you need store this icon too.
Alternatively, if you don't mind the tabs being visible so long as they can't be interacted with, you could use the setTabEnabled function.
ui->tabWidget_2->setTabEnabled( 1, enabled );
ui->tabWidget_2->setTabEnabled( 2, enabled );
ui->tabWidget_2->setTabEnabled( 3, enabled );

Use a pointer to switch between two other pointers in Qt Creator Ui

i do have two Labels in my applications. They are both in each tab. Now i want to witch between those labels by using an pointer that switchs between those labels when tab was changed.
my ui_mainwindow.h defines:
ProLabel *imageLabel;
ProLabel *imageLabel_1;
ProLabel *imageLabel_2;
but only imageLabel_1 and imageLabel_2 are shown in the GUI. I added a third Label (imageLabel) to use it as a variable to switch between the Labels (1/2). So I wrote follwing code in a slot which proves tab changed. So if tab is changed, the other imageLabel_1/2 (pointer) should be used in the hole code, when it says: imageLabel.
my slot when tab changed:
if(tab == 0)
{
this->ui->imageLabel = this->ui->imageLabel_1;
}
else{
this->ui->imageLabel = this->ui->imageLabel_2;
}
I also set the imageLabel_1 whe MainWindow is created. The Window disapear like normal. But if I try to load an image (than it should display the image in the imageLabel) it crashes.
Don't know if it was understandable. Does anybody has an idea if i can handle the pointers like that.. or how to do it different?
Thank you!
Two simpler ways: 1) Change the label contents, either the image or the text it contains, or 2) set the label to hidden with ui->label2->isVisible(false).