i'm trying to set the history on the QFileDialog, but it doesn't seem to appear anywhere.
QFileDialog dialog(parent, caption, path, filter);
dialog.setHistory(history);
dialog.exec();
But i don't see the history in the dialog anywhere. Where should it be? Should it be anywhere? What am I doing wrong here?
edit:
I made this little hack to make it work even with filenames
for(int index = 0; index < files.size(); index++)
{
QFileinfo info(files[index]);
files[index] = info.path();
}
If you open the path selection combo box, you should see them under Recent Places.
Example: The following code
QStringList history;
history << "C:\\temp" << "C:\\Development" << "C:\\Development\\temp";
QFileDialog dialog;
dialog.setHistory( history );
dialog.exec();
leads to this result on my computer (Windows XP 32 bit):
Related
I want to get the text from a QLineEdit, which is in a QFormLayout, to save it to a File. The saving works fine, but I am not able to get the text form the QLineEdit and when I look at it from the Debugger it says "Unable to read Memory". I canĀ“t figure out how to correctly point to the QLineEdit, so that I can get the text.
With this code I want to get the text:
QJsonArray Kegelbuch::saveSettings() {
QFormLayout* formLayout = (QFormLayout*)ui.einstellungenTab->layout();
QJsonArray data;
QLineEdit* settingsEdit;
for (int i = 0; i < formLayout->rowCount(); i++) {
settingsEdit = (QLineEdit*)formLayout->itemAt(i, QFormLayout::ItemRole::FieldRole);
}
return data;
}
How the window looks:
Replace
settingsEdit = (QLineEdit*)formLayout->itemAt(i, QFormLayout::ItemRole::FieldRole);
with
settingsEdit = (QLineEdit*)(formLayout->itemAt(i, FormLayout::ItemRole::FieldRole)->widget());
Background: itemAt() returns a QLayoutItem*, so you need to call QWidget *QLayoutItem::widget() to get the widget.
I'm new to shell programming and having trouble getting the filepath (or really, any information) about which item is being clicked on in a window (desktop or otherwise). I'm following the general path laid out by the answer to Can i use Global System Hooks to capture which file was clicked on? but I'm not having any luck.
The clicking is the smaller issue here, so I've just substituted random values (where I know the desktop is and where a file should be located) for the mouse position. (Regardless, it doesn't work even when I'm trying this out on my mouse's current position).
LVHITTESTINFO hitTest = { 0 };
hitTest.pt.x = 55;
hitTest.pt.y = 230;
hitTest.flags = LVHT_ONITEM;
currWindow = WindowFromPoint(pt);
int index = ListView_HitTest(currWindow, &hitTest);
//cout << index + " index";
//cout << hitTest.iItem + " iltem ";
if (index != -1) {
//char* itemText = new char[256];
std::vector<wchar_t> itemText(1024);
ListView_GetItemText(window, index, 0, &itemText[0], 256);
PIDLIST_ABSOLUTE filepidl;
SFGAOF out;
std::wstring strtext = std::wstring(itemText.begin(), itemText.end());
//cout << " ";
//cout << *(strtext.c_str()) + " ";
HRESULT parse = SHParseDisplayName(strtext.c_str(), NULL, &filepidl, SFGAO_CANDELETE, &out);
if (filepidl != NULL) {
LPTSTR filePath = new TCHAR[MAX_PATH];
BOOL getPath = SHGetPathFromIDList(filepidl, filePath);
cout << *filePath ;
}
}
This is part of my code. I think there's something wrong with how I'm getting the index of the file because it keeps returning 0 but I've been hacking at this for days and am stuck. The MSDN documentation is confusing to me at best.
Any help or insight would be appreciated! I can't find any example code of this online. Thanks!
Using the listview directly like this is not a good idea because Explorer is free to implement the shell view in any way it wants and in Windows 7 and later a Explorer window no longer uses a listview, it uses a custom control by default!
If you only care about the display name and invoking the default action you can use UI Automation, it should work on other types of windows/controls as well, not just a shell file list.
If you need to know the full path and other details you can use the IShellWindows interface. Examples can be found on Raymond Chens blog here and here...
I'm using Qt5. The code is really simple and goes like this:
struct DirEdit {
QLineEdit *lineedit;
QToolButton *button;
QListView *view;
QString dirPath;
QFileSystemModel dirModel;
bool ready {false};
};
Then in the source file:
for (std::size_t i = 0; i != c_lanes; ++i) {
dirEdits[i].view->setModel(&dirEdits[i].dirModel);
dirEdits[i].dirModel.setFilter(QDir::Files);
dirEdits[i].dirModel.setProperty(fIdProp, (uint)i);
}
and then finally when the user chooses a path to display the contents of in a QListView:
DirEdit& de = dirEdits[folderId];
de.dirPath = selectedDirPath;
de.lineedit->setText(selectedDirPath);
de.dirModel.setRootPath(selectedDirPath);
de.view->setRootIndex(de.dirModel.setRootPath(selectedDirPath));
Now when I select /home/srsly (the home folder, this being on a Fedora 25 Linux system), the current folder, where the program is being ran from, gets appended to the beginning of the list. The filters are set to exclude directories as you can see. What can be the reason for this strange behavior?
I have tried to figure this out over the weekend, but to no avail. I cant seem to find an example using QFileSystemWatcher::Files() directly so i thought i would ask.
I have a program that :
lets the user select a 'source' folder.
press a button to start watching that source folder for new files
there is a signal emitted using 'directoryChanged()' where i try to update the count every time a file is added or removed.
I will profess that my implementation of QfileSystemWatcher is probably not correct. but this code is working and does trigger the signal/slot. but the count is always zero...
from mainwindow.cpp...
the signal:
//connect push buttons
QObject::connect(ui->startButton, SIGNAL(clicked()),
this, SLOT(startButtonClicked()));
//link qfilesystemwatcher with signals and slots
QObject::connect(&hotfolder, SIGNAL(directoryChanged(QString)), this, SLOT(hotfolderChanged()));
the slots:
void MainWindow::startButtonClicked(){
//start the file system watcher using the 'source folder button'
//first, get the resulting text from the source folder button
QString sourceText = ui->sourceBtnLineEdit->text();
ui->statusbar->showMessage(sourceText);
//convert the text from source button to a standard string.
string filePath = sourceText.toStdString();
cout << filePath << endl;
//call method to add source path to qfilesystemwatcher
startWatching(sourceText);
}
void MainWindow::hotfolderChanged(){
int fileCount = filesWatched();
ui->statusbar->showMessage(QString::number(fileCount));
}
from magickWatcher.h
#ifndef MAGICKWATCHER_H
#define MAGICKWATCHER_H
#include <QFileSystemWatcher>
#include <mainwindow.h>
//create the qFileSystemWatcher
QFileSystemWatcher hotfolder;
//add folder to qfilesystemwatcher
//starts watching of folder path
int startWatching( QString folder){
hotfolder.addPath(folder);
cout << "hotfolder created!" << endl;
return 0;
}
//get file list of folder being watched
int filesWatched(){
QStringList watchedList = hotfolder.files();
//report out each line of file list
for (int i = 0; i < watchedList.size(); ++i){
cout << watchedList.at(i).toStdString() << endl;
cout << "is this looping?!!" << endl;
}
return watchedList.count();
}
#endif // MAGICKWATCHER_H
How can i use QFileSystemWatcher to get the file count of the watched folder? I know about QDir and its options but want to specifically know how to use QFileSystemWatcher.
I am still wrapping my head around c++ in general so thank you for any advice or tips as well. I think maybe my problem is how i am implementing QFileSystemWatcher.
Some relevant links i have used:
QFileSystemWatcher working only in main()
http://doc.qt.io/qt-5/qfilesystemwatcher.html#files
First let's have a closer look at docs (bold format is mine):
QFileSystemWatcher examines each path added to it. Files that have been added to the QFileSystemWatcher can be accessed using the files() function, and directories using the directories() function.
So, files() only returns a list of files which you have already added to the watcher using addPath() method, NOT a list of files implicitly being watched by adding a directory.
You can get information about files in the watched directory e.g. by using QDir::entryInfoList with filters applicable in your case. At least QDir::Files and possibly QDir::NoDotAndDotDot would make sense.
//get file list of folder being watched
int filesWatched() {
QString folder = "/path/to/hotfolder/";
QDir monitoredFolder(folder);
QFileInfoList watchedList =
monitoredFolder.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
QListIterator<QFileInfo> iterator(watchedList);
while (iterator.hasNext())
{
QFileInfo file_info = iterator.next();
qDebug() << "File path:" << file_info.absoluteFilePath();
}
return watchedList.count();
}
I need to generate a document to print for a number of objects which the user creates dynamically, and I want to print these documents. I wrote following code (generateDocument() takes a reference to the document to add html code):
QPrinter printer;
QPrintDialog popup(&printer);
if (popup.exec() == QDialog::Accepted)
{
for (int i = 0; i < _quiz->getSerieCount(); i++)
{
QTextDocument doc;
generateDocument(doc, _quiz->getSerie(i));
doc.print(&printer);
}
}
When printing to pdf the behaviour is different in linux and windows: On linux this just prints the last generated document, and on windows it prompts to select a new pdf for every generateDocument() call.
Am i supposed to do this differently?
You can add a page break for each serie and then print the document.
Try with the following e.g.
QTextDocument doc;
QTextCursor cursor(&doc);
for (int i = 0; i < _quiz->getSerieCount(); i++)
{
if(i!=0) \\ dont add page break for the first document
{
QTextBlockFormat blockFormat;
blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysAfter);
cursor.insertBlock(blockFormat);
}
// < append _quiz->getSerie(i) contents in the document >
}
doc.print(&printer);
Haven't tested the code, but should work on Windows without any problems I suppose, because I was using it similarly without any issues. Can't comment anything for its behavior on Linux machines. You can modify it better to suit your need.
Hope this Helps.