Disable file name box in QFileDialog - c++

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.

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");

QT QIcon properties for custom widget in designer

I have been working for a little while now on creating a QT custom designer widget for GUI menus. The idea being that you simply drag it into the designer, select the number of frames you'd like, how many buttons per frame, etc. and it generates and sizes everything for you.
The way the widget is structured there are properties to configure each button for the frame you are in. For example, you would use the button0Text field to enter text under Button0 while editing in frame 0, then use it again to edit Button0 which is in frame 1. Both buttons would retain the individual changes for each frame.
The Problem
Normally when I switch frames all of my properties are updated to reflect the status of the frame. The exception being QIcon. The correct icon is retained in the actual graphical representation and builds correctly, however the file path in the property list is always of the last edited for that property. I think this will be extremely confusing to an end user and I have found no way to fix it. So for example, if I set text and icons in frame 0 then switch to frame 1 the text in the property list will update to reflect the state of frame 1 but the icon path names will still show my last edit in frame 0 and not the actual icon in frame 1.
I have tried things as simple as:
setProperty("button0Icon", getButton0Icon());
That code works on properties like text, but not for the icon. I try executing it immediately after changing frames.
I have also tried:
#ifndef Q_WS_QWS
QDesignerFormWindowInterface *form = QDesignerFormWindowInterface::findFormWindow(this);
if(form){
QDesignerFormEditorInterface *editor = form->core();
QExtensionManager *manager = editor->extensionManager();
QDesignerPropertySheetExtension *sheet;
sheet = qt_extension<QDesignerPropertySheetExtension*>(manager, this);
int propertyIndex = sheet->indexOf("button0Icon");
sheet->setChanged(propertyIndex, true);
sheet->setProperty(propertyIndex, getButton0Icon());
}
#endif
And:
int propertyIndex = this->metaObject()->indexOfProperty("button0Icon");
QMetaProperty property = this->metaObject()->property(propertyIndex);
property.write(this, QIcon());
Nothing seems to update the property list in the designer.
I have all properties, including all QIcon properties properly declared in the header file with Q_PROPERTY and assigned getter and setter functions.
To be clear, the icon values are indeed retained through each frame when compiled. So it is functioning, just unclear for most users.
If anyone has any experience with this or any ideas please let me know. Thanks.
I have discovered that QIcon does not store file names/paths. The file names are only used for the creation of the QIcon. I think this is most likely the reason I do not get any feedback in the Property Browser for my QIcon properties.
Instead I have chosen to hide this property in the designer and add three new ones. Three QUrl properties, each of which is used to supply an image file. I use three because I want to construct a QIcon that contains Modes/States for normal, disabled, and pressed operations.
I take each of these QUrls and save them in QStringLists behind the scenes so their values are stored. I then construct my QIcon using the file names provided from the QUrls.
I would much prefer to be using the native QIcon in the designer for this, any thoughts or feedback are appreciated.

How can I add a context sensitive help button for a specific dialog control

I have an MFC app (10 year old app), which has context sensitive help for each dialog. I want to add help on a specific combobox, as well as a little question mark button next to this control. Users can either select the combobox and hit F1, or they can click on the button next to the combobox, and it will jump to a help page that is specifically about this combobox, rather than general help for the whole dialog.
In the dialog resource properties, I have set "Context Help" to True.
In the combobox properties, I've set "Help ID" to True.
In myapp.hpp, I have added "HIDC_MYCOMBOBOX = mycombobox_help.htm" to the [ALIAS] section, and included the resource.hm file in the [MAP] section.
Again in app.hpp file, the dialog uses "HIDD_MYDIALOG =
mydialog_help.htm"
Yet selecting the combobox and pressing F1 still brings up mydialog_help.htm, instead of mycombobox.htm.
What am I missing to use a separate help page for the control?
Is it possible to redirect the control to an anchor in the main page? Something, along the lines of...
HIDC_MYCOMBOBOX = mydialog_help.htm#mycombobox
I have added a "?" button to run the following code, but this also doesn't give the context for the control, and just opens the mydialog_help.htm.
HELPINFO lhelpinfo;
lhelpinfo.cbSize = sizeof(lhelpinfo);
lhelpinfo.iContextType = HELPINFO_WINDOW;
lhelpinfo.iCtrlId = IDC_BALANCING_METHOD;
lhelpinfo.hItemHandle = GetDlgItem(IDC_BALANCING_METHOD)->m_hWnd;
lhelpinfo.dwContextId = HIDC_BALANCING_METHOD;
lhelpinfo.MousePos = POINT();
CDialog::OnHelpInfo(&lhelpinfo);

Qt get active text box

If I have two text boxes in my main window how can I check which one is active/being used by the user?
You can use QApplication::focusWidget() function to see which widget currently has focus.
Or you can use the QWidget::hasFocus() function to see if your text box has focus.
edit_A = new QTextEdit(this);
edit_B = new QTextEdit(this);
.
.
.
void MyClass::someFunction()
{
if(edit_A->hasFocus())
//edit_A is being used
else if(edit_B->hasFocus())
//edit_B is being used
}
The previous answer is right but just in case you want to monitor more components you can use
QWidget * QApplication::focusWidget ()
to get widget with focus.
This approach will allow you to make cleaner code if you wanted to have more widgets monitored. Instead of having a ladder of ifs just use some kind of look-up table to choose what action to take.

Taking data from a Dialog in Qt and using it in a Ui

So I'm making a text editor using Qt and right now I have a button that opens a dialog called "Format text". I want it to work kind of like the dialog in notepad called "font" where you select a few text attributes from some drop down lists and it shows you what your text will look like. Right now I have it working where you can select the font style, font color, and font size and hit preview and it shows you in a box in the dialog what your text will look like. However, I have a button called "okay" which is supposed to change the highlighted text or the text you are about to type, but I can't figure out how to display those changes on the main window. The .ui files are private and a lot of the already made functions and pointers are the same in every ui file so if I change the ui file to pubic I have to change a whole bunch of things. Can anyway give me a simple answer? I'm trying to do this with as little confusion as possible. More coding and less confusion is better than less coding and more confusion for someone of my skill level. Sorry that this is all one giant paragraph and that I didn't provide any code, but I didn't think the code was necessary, however if you do need some of the code i'd be happy to share it.
Thank you for your help and your time. I hope you all have a nice evening.
QDialog have a signal called finished(), you can connect this signal with your slot. To accomplish your work, pass a QSettings or for simplicity QStringList to dialog settings (responsible for changing font, color ...), the QStringList will save user defined settings, after closing the dialog, iterate through QStringList member to alert Main window.
A pseudo code will look like this
Class Editor:
Editor::Editor()
{
TextSettings textSettings;
textSettings.setSettings(settings); // settings is a member
connect(textSettings, &finished(int)), this, SLOT(alertEditor(int)))
}
Editor::alertEditor(int s)
{
if(s == 0)
{
for (int i = 0; i < settings.size(); ++i)
settings.at(i).toLocal8Bit().constData(); // extract various user settings
}
}
Class TextSettings:
TextSettings::TextSettings(QStringList settings)
{
settings << ui->combobox->currentItem(); // font name as example
}