PDA

View Full Version : File icon to graphic view



been_1990
26th May 2009, 02:34
How can I retrieve a file icon and draw it on a QGraphicsView?

aamer4yu
26th May 2009, 05:19
Where do you want to read the file icon from ?
As for adding icon in graphics view, you can simply set icon to a QLabel and add the label to graphics scene.

Alternatively you can use QGraphicsPixmapItem :)

been_1990
26th May 2009, 14:00
I want to read it from any computer file, folder and shortcut on a computer.
Lets say: select "myapp.exe", get its icon, and display in QGraphicsView.

been_1990
28th May 2009, 17:57
So... anyone?

faldzip
28th May 2009, 20:39
use QFileIconProvider

been_1990
29th May 2009, 18:25
After realy trying to find out how to set up a QGraphicsView, I give up. I get many errors and no image renders. Does anyone have a sample code?

faldzip
29th May 2009, 23:49
This:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
gv = new QGraphicsView(this);
setCentralWidget(gv);
QGraphicsScene *scene = new QGraphicsScene(gv);
scene->setSceneRect(0, 0, 500, 500);
gv->setScene(scene);

QGraphicsRectItem *ritem = new QGraphicsRectItem(scene->sceneRect());
scene->addItem(ritem);

// first way to add a pixmap
QGraphicsPixmapItem *pitem = new QGraphicsPixmapItem(QPixmap(":/img/Save.png"));
scene->addItem(pitem);
pitem->setPos(100, 100);

// second way
scene->addPixmap(QPixmap(":/img/Find.png"))->setPos(200, 200);
}
will result in this:

been_1990
30th May 2009, 23:02
Thanks! That worked. But now I have another problem:

QString file = "C:\\Users\\administrator\\Downloads\\ethereal-setup-0.99.0.exe";
QFileInfo fileInfo( file );
QFileIconProvider fileIconProvider;
QIcon appIcon = fileIconProvider.icon( fileInfo );
Thats code I found here in the Forum.
Then I do the following:

QGraphicsPixmapItem *pitem2 = new QGraphicsPixmapItem(QPixmap(appIcon.pixmap(2,QIcon ::Normal,QIcon::On)));
scene->addItem(pitem2);
pitem2->setPos(100, 100);
pitem2->setFlag(QGraphicsItem::ItemIsMovable,true);
But nothing renders on the screen. An idea why?

been_1990
30th May 2009, 23:03
Thanks! That worked. But now I have another problem:

QString file = "C:\\Users\\administrator\\Downloads\\ethereal-setup-0.99.0.exe";
QFileInfo fileInfo( file );
QFileIconProvider fileIconProvider;
QIcon appIcon = fileIconProvider.icon( fileInfo );
Thats code I found here in the Forum.
Then I do the following:

QGraphicsPixmapItem *pitem2 = new QGraphicsPixmapItem(QPixmap(appIcon.pixmap(2,QIcon ::Normal,QIcon::On)));
scene->addItem(pitem2);
pitem2->setPos(100, 100);
pitem2->setFlag(QGraphicsItem::ItemIsMovable,true);
But nothing renders on the screen. An idea why?

faldzip
31st May 2009, 07:29
I think your problem might be here:


QPixmap(appIcon.pixmap(2,QIcon::Normal,QIcon::On))

First of all QIcon::pixmap() already returns QPixmap so invoking QPixmap constructor again is not necessary (but it is not an error). The strange thing in your code is that "2"... it means that you want and pixmap of size 2x2... not so big for me. Try changing it to 32 for example and check if it helps.
For me this works fine:


QString filename = QFileDialog::getOpenFileName(this,
tr("Choose file..."),
qApp->applicationDirPath());
QFileIconProvider fip;
QIcon icon = fip.icon(QFileInfo(filename));
scene->addPixmap(icon.pixmap(32, QIcon::Normal, QIcon::On))->setPos(50, 50);

been_1990
1st June 2009, 15:53
Thanks! Sorry for the double post. I have a new issue on icon grabbing with diferrent file types, should I open a new thread?