Automatically saving a file with QFileDialog - c++

I have to automate a test using QTest, Qt, C++:
I write text in a tab (part of tabwidget) and then try to close it, afterwards a QFileDialog appears ( because I made changes to the plaintext in the tab), I try to "catch" the QFileDialog like this:
QWidgetList topWidgets = QApplication::topLevelWidgets();
foreach (QWidget *w, topWidgets) {
if (QFileDialog *fd = qobject_cast<QFileDialog *>(w)) {
fd->setFileMode(QFileDialog::ExistingFiles);
fd->selectFile("/tmp/test.txt");
}
}
After getting the QFileDialog object I want my changes from the tab to be saved in the file "test.txt" which I created before in the tmp directory. When I execute this nothing happens, the QFileDialog pops up, but test.txt is not selected and not saved, how can I achieve this?

The selectFile method does not work if the filedialog is visible and if the focus is set to the line edit widget. From the qfiledialog.cpp (QT 5.2):
if (!isVisible() || !d->lineEdit()->hasFocus())
d->lineEdit()->setText(file);
For our automated tests, we just hide the filedialog for a moment, call selectFile() and show it again

Try this:
QWidgetList topWidgets = QApplication::topLevelWidgets();
foreach (QWidget *w, topWidgets) {
if (QFileDialog *fd = qobject_cast<QFileDialog *>(w)) {
fd->hide();
fd->selectFile("/tmp/test.txt");
fd->show();
fd->exec();
}
}

Related

qt/c++ context menu - disable an item

I'm currently developing an app such as the browser using Qt and c++.
I have create a contextual menu to allow on right click action such as delete, rename and add folder.
void MyTreeWidget::createContextMenu() {
contextMenu = new QMenu();
setContextMenuPolicy(Qt::ActionsContextMenu);
addFolderAction = new QAction("Add Folder", contextMenu);
addAction(addFolderAction);
connect(addFolderAction, SIGNAL(triggered()),this,SLOT(onAddFolderActionTree()));
deleteAction = new QAction("Delete", contextMenu);
addAction(deleteAction);
connect(deleteAction, SIGNAL(triggered()),this,SLOT(onDeleteAction()));
RenameAction = new QAction("Rename", contextMenu);
addAction(RenameAction);
connect(RenameAction, SIGNAL(triggered()),this,SLOT(onRenameAction()));
}
This is working fine. This contextual menu is used when you select a file or folder in my treewidget and make a right click. My issue is that I propose the "add folder" option even if I select a file. You can't create a folder inside a file.
What I want is to disable the option when a file is selected and enable it when it's a folder.
I can know if it's file or folder by getting the TreeWidgetItem class I have overloaded:
Thanks
You can disable QAction. In this case "Add Folder" menu item will be disabled:
addFolderAction->setEnabled(false);
Use the QAction::setEnabled(bool) method on your 'addFolderAction'.
One way to use it is like this:
void
MyTreeWidget::updateMenuActions()
{
if(!contextMenu)
return;
bool addFolderEnabled = <check TreeWidgetItem here to enable / disable>;
addFolderAction->setEnabled(bEnabled);
}
Call the updateMenuActions() method just before you display the context menu.
I actually prefer the code below in case you have situations where you can have NULL pointers to actions (for cases where you don't even add them):
void
MyTreeWidget::updateMenuActions()
{
if(!contextMenu)
return;
bool addFolderEnabled = <check TreeWidgetItem here to enable / disable>;
updateAction(addFolderAction, bEditEnabled);
}
void
MyTreeWidget::updateAction(QAction* pAction, const bool& bEnabled)
{
if(pAction)
pAction->setEnabled(bEnabled);
}
Enjoy.

How to modify the -webkit-scrollbar styles in a QWebView

Webkit provides special css properties for styling scrollbars, for example:
::-webkit-scrollbar-track {
background-color:white;
}
Normally I'd put these inside a <style> tag inside <head>, but unfortunately QWebElement doesn't seem to be able to modify anything inside <head>. I can use the setHtml() function to specify initial styling, but not modify it later. Is there an alternative way to apply CSS styles to the scrollbars in a QWebFrame?
Is possible using QWebSettings::setUserStyleSheetUrl, see example:
const QString path = PATH_OF_CSS_FILE;
QWebSettings *settings = QWebSettings::globalSettings();
settings->setUserStyleSheetUrl(QUrl(path));
Example
If dynamic CSS is a string, you can create a method and use QTemporaryFile, like this:
void MainWindow::setStyle(const QString data)
{
QTemporaryFile file;
if (file.open()) {
const QString path = file.fileName();
QWebSettings *settings = QWebSettings::globalSettings();
settings->setUserStyleSheetUrl(QUrl(path));
}
}
Usage:
setStyle("::-webkit-scrollbar-track { background-color:white;}")
If needs load dynamic file, you can create an alternative method like this:
void MainWindow::setStyle(const QUrl url)
{
QWebSettings *settings = QWebSettings::globalSettings();
settings->setUserStyleSheetUrl(url);
}
Using QRC
Is part of the answer is just a tip for other implementations;
You can use resources (QRC) in your project for put default stylesheet for all QWebViews, see example:
Click right button in your project > Add New ... > Qt > Qt Resource File > Put name "resources.qrc"
Click right button in "resources.qrc" > Open in Editor
Put a CSS file with name scrollbar.css (css file must be in the same folder as your project).
Put this in your "main.cpp":
#include <QWebSettings>
#include <QUrl>
...
const QString path = "qrc:/scrollbar.css";
QWebSettings *settings = QWebSettings::globalSettings();
settings->setUserStyleSheetUrl(QUrl(path));

Qt: add a file selection field on the form (QLineEdit and "browse" button)

I need to display QLineEdit with "Browse" button at my form. When user clicks button, QFileDialog should be opened, and so on.
This is pretty common thing, but I can't find ready-made solution for that. I expected in Qt Designer some widget like QFileSelect, or something like that, but found nothing similar.
Should I implement it by hand? Or, what is the correct way to do this?
Should I implement it by hand? Or, what is the correct way to do this?
Yes, I agree with you that it is a common thing, but unfortunately you will need to implement this yourself. The good news is that you can do this easily by something like this:
MyMainWindow::createUI()
{
label = new QLabel("foo");
button = new QPushButton("Browse");
connect(button, SIGNAL(clicked()), SLOT(browse()));
layout = new QHorizontalLayout();
layout->addWidget(label);
layout->addWidget(button);
setLayout(layout);
}
void MyMainWindow::browse()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Find Files"), QDir::currentPath());
if (!directory.isEmpty()) {
if (directoryComboBox->findText(directory) == -1)
directoryComboBox->addItem(directory);
directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
}
}

How to read a text from a text field from c++

I have a text field and a button purely designed in c++(without importing a qml doc). How do i read the text from the text field when I click the buton.
I am unable to find a function associated for that.
To hook up the button to a method, use the following code:
button = new Button();
texField = new TextField();
connect(button, SIGNAL(clicked()), this, SLOT(onClicked());
Then define the onClicked slot as so:
void ClassName::onClicked() {
qDebug() << textField->text(); //print the textField's text
}
For this to work, this method has to be marked in the class as a Q_SLOT and the object itself must be marked as a Q_OBJECT.

Getting the name and location of selected file Qt

I have a program in which I have a button to get File Dialog like
How can I select a file, get the file name and location, and save that to a string displayed in the ui.The signalclicked(), emitted from the button, is connected to the slot fileSELECT().
........
void MainThread::fileSELECT(){
QString fileName = QFileDialog::getOpenFileName(this,tr("Select video"),"d:\\BMDvideos",tr("Video files (*.avi)"));
}
so when I select an .avi file, how do I get its location in fileName displayed like
d:\BMDvideo\videFile.avi
so I thinks that I got it now. my first code was completly wrong.
void MainThread::fileSelect(){
QString fileName = QFileDialog::getOpenFileName(this,tr("Select video"),"d:\\BMDvideos",tr("Video files (*.avi)"));
QLabel *testLabel = new QLabel(fileName);
BOX->addWidget(testLabel);
}
I can see now the path of the selected file
To get the folder path, you can use QFileDialog::getExistingDirectory, and to get the file-name use QFileDialog::getOpenFileName