Results 1 to 11 of 11

Thread: File icon to graphic view

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default File icon to graphic view

    How can I retrieve a file icon and draw it on a QGraphicsView?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: File icon to graphic view

    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

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: File icon to graphic view

    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.

  4. #4
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: File icon to graphic view

    So... anyone?

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: File icon to graphic view

    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #6
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: File icon to graphic view

    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?

  7. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: File icon to graphic view

    This:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindow)
    3. {
    4. ui->setupUi(this);
    5. gv = new QGraphicsView(this);
    6. setCentralWidget(gv);
    7. QGraphicsScene *scene = new QGraphicsScene(gv);
    8. scene->setSceneRect(0, 0, 500, 500);
    9. gv->setScene(scene);
    10.  
    11. QGraphicsRectItem *ritem = new QGraphicsRectItem(scene->sceneRect());
    12. scene->addItem(ritem);
    13.  
    14. // first way to add a pixmap
    15. QGraphicsPixmapItem *pitem = new QGraphicsPixmapItem(QPixmap(":/img/Save.png"));
    16. scene->addItem(pitem);
    17. pitem->setPos(100, 100);
    18.  
    19. // second way
    20. scene->addPixmap(QPixmap(":/img/Find.png"))->setPos(200, 200);
    21. }
    To copy to clipboard, switch view to plain text mode 
    will result in this:
    Attached Images Attached Images
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. #8
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: File icon to graphic view

    Thanks! That worked. But now I have another problem:
    Qt Code:
    1. QString file = "C:\\Users\\administrator\\Downloads\\ethereal-setup-0.99.0.exe";
    2. QFileInfo fileInfo( file );
    3. QFileIconProvider fileIconProvider;
    4. QIcon appIcon = fileIconProvider.icon( fileInfo );
    To copy to clipboard, switch view to plain text mode 
    Thats code I found here in the Forum.
    Then I do the following:
    Qt Code:
    1. QGraphicsPixmapItem *pitem2 = new QGraphicsPixmapItem(QPixmap(appIcon.pixmap(2,QIcon::Normal,QIcon::On)));
    2. scene->addItem(pitem2);
    3. pitem2->setPos(100, 100);
    4. pitem2->setFlag(QGraphicsItem::ItemIsMovable,true);
    To copy to clipboard, switch view to plain text mode 
    But nothing renders on the screen. An idea why?

  9. #9
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: File icon to graphic view

    Thanks! That worked. But now I have another problem:
    Qt Code:
    1. QString file = "C:\\Users\\administrator\\Downloads\\ethereal-setup-0.99.0.exe";
    2. QFileInfo fileInfo( file );
    3. QFileIconProvider fileIconProvider;
    4. QIcon appIcon = fileIconProvider.icon( fileInfo );
    To copy to clipboard, switch view to plain text mode 
    Thats code I found here in the Forum.
    Then I do the following:
    Qt Code:
    1. QGraphicsPixmapItem *pitem2 = new QGraphicsPixmapItem(QPixmap(appIcon.pixmap(2,QIcon::Normal,QIcon::On)));
    2. scene->addItem(pitem2);
    3. pitem2->setPos(100, 100);
    4. pitem2->setFlag(QGraphicsItem::ItemIsMovable,true);
    To copy to clipboard, switch view to plain text mode 
    But nothing renders on the screen. An idea why?

  10. #10
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: File icon to graphic view

    I think your problem might be here:
    Qt Code:
    1. QPixmap(appIcon.pixmap(2,QIcon::Normal,QIcon::On))
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QString filename = QFileDialog::getOpenFileName(this,
    2. tr("Choose file..."),
    3. qApp->applicationDirPath());
    4. QIcon icon = fip.icon(QFileInfo(filename));
    5. scene->addPixmap(icon.pixmap(32, QIcon::Normal, QIcon::On))->setPos(50, 50);
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  11. #11
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: File icon to graphic view

    Thanks! Sorry for the double post. I have a new issue on icon grabbing with diferrent file types, should I open a new thread?

Similar Threads

  1. Dropping file over app icon in MAC
    By dev_maximaa in forum Qt Programming
    Replies: 0
    Last Post: 20th March 2009, 05:40
  2. Obtaining a Windows file icon using fromWinHBITMAP()
    By DIMEDROLL in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2008, 11:50
  3. Setting OS Specified Icon on the File?
    By vishal.chauhan in forum Qt Programming
    Replies: 4
    Last Post: 31st May 2008, 12:37
  4. Set QApplication icon without using the rc/qrc file ?
    By Nyphel in forum Qt Programming
    Replies: 8
    Last Post: 23rd March 2007, 15:06
  5. set Icon to .app file
    By vishal.chauhan in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2007, 21:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.