I am a newbie in QT. I am working on a app where I need to display the FileSystem using a treeview.
Basically I have a widget in my .ui file on which I have put a Treeview. Then in my .cpp file I have written the following code:
model = new QFileSystemModel(this);
model->setRootPath(QDir::homePath());
ui->treeView->setModel(model);
In my .h file I have put the following:
QFileSystemModel *model;
When I run the app, it displays the file system inside the treeview but it also shows Name, Type, Size, DateModified above it. I want to get rid of these.
Here is the sample Image:
How can I achieve it?
I think that QTreeView::hideColumn does it.
Related
I am creating a Qt application and I have an image that I want to use for a button instead of text. Unfortunately all that shows up is an empty button.
I've tried two different methods to get it to show up with the same results for both methods.
Code for Method 1:
ui->setupUi(this);
QPixmap pix(":/svg/resources/menu.svg");
int w = ui->menuButton->width();
int h = ui->menuButton->height();
ui->menuButton->setMask(pix.scaled(w,h,Qt::KeepAspectRatio).mask());
I found the info for the second method here: Adding image to QPushButton on Qt
Code for Method 2:
ui->setupUi(this);
QIcon icon(":/svg/resources/menu.svg");
ui->menuButton->setIcon(icon);
Could someone please help me figure out why my image isn't showing up and the button is just empty?
In my project using .svg images as button icons are no problem, maybe the button size is missing, try:
ui->menuButton->setIcon(QIcon(":/svg/resources/menu.svg"));
ui->menuButton->setToolTip("optional tooltip");
ui->menuButton->setFixedSize(QSize(28,28));
Assuming you stored your icons correctly in a resource file. If not, create a new:
right click on your top project folder (in the project-tree) -> Add new.. -> choose Qt on the left an Qt Resource File on the right window -> a new Window apears.
Add Prefix -> Add Files (your icon)
You use .svg image format. Are you sure your application load image format plugin for .svg? Image plugins must be in directory "imageformats" in current directory of your application. Avaliable plugins you can find in Qt directory .../Desktop/Qt/<version>/<mingw or msvc>/plugins/imageformats
I am trying to filter file system model to show only files with extension .ncr (NCReport templates). The view instead shows all files. Any ideas how to make the filtering work? Thanks.
(I realize there's plenty other clumsiness here, suggestions welcome.)
fsmodel = new QFileSystemModel(this);
connect(fsmodel,SIGNAL(rootPathChanged(QString)),this,SLOT(fileSystemModelRootSetSuccessfully()));
// this call is required on windows to show anything in file view
QModelIndex rootIndex=fsmodel->setRootPath(reportdirstring);
// root index could be removed
Q_UNUSED(rootIndex);
fsmodel->setReadOnly(true);
ui->reportTemplateDirView->setModel(fsmodel);
ui->reportTemplateDirView->setRootIndex(fsmodel->index(reportdirstring));
ui->reportTemplateDirView->expandAll();
ui->reportTemplateDirView->header()->hide();
// selecting the first file entry with selectFileInDirView(); requires the qtreeview to be sorted
// sort in desc order since that is the only way to get the first item selected?
ui->reportTemplateDirView->sortByColumn(0,Qt::AscendingOrder);
fsmodel->sort(0,Qt::AscendingOrder);
QStringList filters;
filters << "*.ncr";
fsmodel->setNameFilters(filters);
fsmodel->setNameFilterDisables(false);
// hide report template directory view extra columns,
//type?
ui->reportTemplateDirView->setColumnHidden(1,true);
//size?
ui->reportTemplateDirView->setColumnHidden(2,true);
//date
ui->reportTemplateDirView->setColumnHidden(3,true);
#if QT_VERSION >= 0x040700
// as soon as QFileSystemModel has parsed the entire directory tree, tell the QTreeView to expand its hierarchy
// note that if there are a lot of files, this could be too inefficient.
// if problems arise, consider commenting this out or using a QDirModel, which could be equally inefficient though.
connect(fsmodel,SIGNAL(directoryLoaded(QString)),ui->reportTemplateDirView,SLOT(expandAll()));
connect(fsmodel,SIGNAL(directoryLoaded(QString)),this,SLOT(selectFileInDirView()));
#endif
// show a fake folder name + icon at the top of the folder tree of report template directory
QFileIconProvider iconProvider;
QIcon folderIcon=iconProvider.icon(QFileIconProvider::Folder);
ui->reportTemplatesLabel->setPixmap(QPixmap(folderIcon.pixmap(QSize(16,16))));
As Alexander noted, this actually works. The issue was that I was setting the filters to empty elsewhere. ^.^
Is there any way to load qt compiled c++ code in maya?
//example code
void MainWindow::on_pushButton_clicked()
{
ui->labell->setText("Hello");
}
actually i was created basic ui with text and push button, what i want is text should change to hello when i push button and i achieved that. so this connections were made with above code, after compiling this all works fine but when i load ui file in maya and i pushes the button text doesn't changes because actually code was written in c++. so, is there any alternative to load that code too along with ui file?
thank you,
Anvesh Chary
To load a .ui file in Maya, I've previously done this in python, I'm not sure about C++ but I don't believe maya interprets C++ directly anyway (I could be wrong there).
import maya.cmds as cmds
ve = cmds.about(version=True)
conv = "%s"%ve
versionOutput = float(conv[0:4])
def mayaVers():
cmds.warning("You're using Maya %s! You need to be using Maya 2011 or greater to be compatible with this script.\n" % conv);
def loadUIWindow():
if versionOutput >= 2011:
if (cmds.dockControl('dockUIWindow', exists=True)):
cmds.deleteUI('dockUIWindow')
scriptsDirectory = cmds.internalVar(usd=True)
UIWindow = cmds.loadUI(uiFile=scriptsDirectory + "/uifilename.ui")
dockSoftMod = cmds.dockControl('dockUIWindow',area="left", content='uiwindowname', label="")
else:
mayaVers()
loadUIWindow()
Here's how I've done it in the past, if you're just looking to source a UI file into the Maya session, this is how it can be done.
Obviously you'll need to either put your ui file in the scripts directory, or change the uiFilePath to your file.
Also, the content flag in the dockControl is important, this needs to be the name of the window or control that you're trying to dock. Let's say you have called your UI file wrapper 'win', the content flag would need to be the same.
EDIT
After you load the UI file, you can edit any element in the window if you know it's name.
cmds.button('ParentBtn', edit=1, command="parentObject()")
Hope this helps.
I wrote this on official forums of Qt, but it seems dead, so I am going to copy-paste it here.
I am writing small program for copying files. I use QTreeView and I have inherited from QFileSystemModel, so I was able to add checkboxes to every row in the QTreeView. I also use setNameFilters method connected with QLineEdit, so user can specify what file extensions he wants to display in the QTreeView. I have spotted the following behavior:
1) When I run the program and enter extensions to filter (without touching any node from the QTreeView) everything works fine and files with extensions I have provided are only displayed (and folders of course). When I change the extensions and the view is refreshed, on my "C:/" drive everything is updated and only new set of extensions is displayed. When I expand some other drive that I didn’t touch before, it also shows files correctly.
2) When I run the program and expand let say my "C:/" and "D:/" drives I see all directories and files (expected behavior). Then I write some extensions and the view is refreshed. I expand "C:/" drive and everything works fine, only files with extensions I have provided are displayed. Then I go to "D:/" drive and here is the problem. It displays all files. It ignores the filters I have provided. When I open the "E:/" drive that I have not opened before, the files are filtered correctly as in "C:/" drive.
I have concluded, that this behavior has something to do with setRootPath method, because for my QTreeView only in "C:/" drive the filters are working correctly. All other drives that were expanded before change of filters don’t work. Those not expanded work just fine.
The question is: How to get this working, so after user changes the filters and reset() method is fired, the whole QTreeView is refreshed and not only root path and not-expanded elements? Maybe there exists some root path that have all the drives as children and it will work as expected? Or maybe I should make some virtual folder in the QTreeView called "MyComputer" and set it to be a parent for all the drives? But how to get list of all the available drives?
I hope that what I wrote is clear for you and you can help me to get this working.
Edit:
Adding some code that is relevant. If you need more just ask.
//setting up the model and view
QString rPath = "C:/";
rTree_model = new TreeModel(this); //TreeModel inherits from QFileSystemModel
rTree_model->setRootPath(rPath);
ui->rTree->setModel(rTree_model); //applies the model for the qtreeview (ui->rTree)
//(...)
//action when extensions were provided by user
QString extensions = QString(ui->extensionBox->text()); //gets extensions provided by user
QStringList filters;
if(extensions.length() > 0) {
filters = extensions.split(";", QString::SkipEmptyParts); //splits extensions provided with ';' as separator
rTree_model->setNameFilters(filters); //applies filters
ui->rTree->reset(); //resets the view
}
Try changing your root path to My Computer instead of C:/. It seems to work with QFileSystemModel in Windows 7 x64 and Qt 4.8.2, but I can't guarantee anything for other platforms.
rTree_model = new TreeModel(this);
QString rPath = model->myComputer().toString(); //causes the QFileSystemWatcher to watch every drive?
rTree_model->setRootPath(rPath);
ui->rTree->setModel(rTree_model);
I want to create a new folder in Qml…So I found out it via Qt…..so i want to integrate this below Qt C++ with Qml…..How is it possible…
QDir dir(“path/to/dir”);
if (!dir.exists())
{
dir.mkpath(”.”);
}
Or else,is there any options for creating new folder directly in Qml…Please suggest a solutions.Thanks in advance.
There's no way to create a directory directly from QML nor JavaScript. You will have to create an object in C++ and "export" it (make visible) to QML. Than you can call this object's method from your QML code and it will create the directory.
The basic idea of connecting C++ and QML is covered here:
Reading and writing files in QML
The only thing you'd have to change is to exchange write method for createDir (or whatever you want) and insert your code.
Another way to do it is to set contextProperty ex.
QQmlContext *context;
context = viewer.rootContext();
context->setContextProperty("DirManager", &dManager);
(where DirManager is your class) and use macro Q_INVOKABLE before the return type of your method.