Results 1 to 9 of 9

Thread: Hex viewer in qt?

  1. #1
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Hex viewer in qt?

    Hello All,

    I m using Qt 4.1.5 on my MAC.
    I want to create a hex viewer in qt which shows the file in the hex form.

    If any body know how I can do this than plz help me out.

    Thanks.
    Last edited by vishal.chauhan; 16th July 2007 at 09:48.

  2. #2
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Hex viewer in qt?

    you can make hexviewer by using text edit event filter. I am send you code of hex viewer from my project.
    Qt Code:
    1. bool ProgramMemoryWindow::eventFilter(QObject *target, QEvent *event)
    2. {
    3. if (target == m_pProgamWindow)
    4. {
    5. if (event->type() == QEvent::KeyPress)
    6. {
    7. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    8.  
    9. if ((keyEvent->key() == Qt::Key_unknown)||(keyEvent->key() == 0))
    10. {
    11. return true;
    12. }
    13.  
    14. QString str1 = keyEvent->text();
    15. if (str1)
    16. {
    17.  
    18. char ch = *(str1.ascii());
    19. bool res = true;
    20. if (((ch <= '9' && ch >= '0') | (ch <= 'F' && ch >= 'A') |
    21. (ch <= 'f' && ch >= 'a')))
    22. {
    23. int para,index;
    24. static char chcnt=0;
    25. chcnt++;
    26. m_pProgamWindow->getCursorPosition(&para,&index);
    27. if ((index % 3 == 0))
    28. {
    29. m_pProgamWindow->setCursorPosition(para,index+1);
    30. chcnt = 0;
    31. }
    32. if ((index == 54) && (para == 15))
    33. {
    34. return true;
    35. }
    36. m_pProgamWindow->getCursorPosition(&para,&index);
    37. char value;
    38. getAddressFromPosition(para,index,ch,&value);
    39. char str[4];
    40. if ((value<0x7f)&&(value>0x1F))
    41. str[0]= value;
    42. else
    43. str[0] = 0x2e;
    44. str[1]= '\0';
    45. unsigned int asciipos = ((index - 7)/3)+55;
    46. m_pProgamWindow->setSelection(para,asciipos,para,asciipos+1,0);
    47. m_pProgamWindow->removeSelectedText(0);
    48. m_pProgamWindow->setColor(QColor(255,0,0));
    49. m_pProgamWindow->insertAt(str,para,asciipos);
    50. m_pProgamWindow->selectAll(false);
    51. m_pProgamWindow->setCursorPosition(para,index);
    52. m_pProgamWindow->setSelection(para,index,para,index+1,0);
    53. m_pProgamWindow->setColor(QColor(255,0,0));
    54. return QWidget::eventFilter(target, event);
    55. }
    56. }
    57. if ((keyEvent->key() == Qt::Key_Left )||(keyEvent->key() == Qt::Key_Up )||
    58. (keyEvent->key()==Qt::Key_Right )||(keyEvent->key()==Qt::Key_Down ))
    59. {
    60. return QWidget::eventFilter(target, event);
    61. }
    62. if ((keyEvent->key()==Qt::Key_Enter)||(keyEvent->key()==Qt::Key_Return))
    63. {
    64. saveContents();
    65. return true;
    66. }
    67. if ((keyEvent->key()==Qt::Key_Escape))
    68. {
    69. revertData();
    70. return true;
    71. }
    72. if ((keyEvent->key()==Qt::Key_PageDown))
    73. {
    74. onDownScrollClicked();
    75. return true;
    76. }
    77. if ((keyEvent->key()==Qt::Key_PageUp))
    78. {
    79. onUpScrollClicked();
    80. return true;
    81. }
    82. return true;
    83. }
    84. if ((event->type()== QEvent::Wheel))
    85. {
    86. return true;
    87. }
    88. if(!((event->type()== 10)||(event->type()== 11)||(event->type()== 24)||(event->type()== 25)||(event->type()== 12)))
    89. return QWidget::eventFilter(target, event);
    90.  
    91. }
    92.  
    93. return QWidget::eventFilter(target, event);
    94.  
    95. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 16th July 2007 at 13:05. Reason: changed [html] to [code]

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Hex viewer in qt?

    I guess he did not meant writing hex but displaying files as hex... the only way to achieve this is to create a custom window. You can either try to do everything from scratch or fill a QTextDocument with hex digits (quick and dirty hack (c) ). if the QTextDocument hack looks awful (because it really IS) then you'll have to reimplement a QAbstractScrollArea (consider taking some inspiration from QTextEdit sources...) and especially the paint event which should not be that complicated. The most important thing you'll have to take into account before starting working on such a widget is deciding HOW you'll store the content to view as hex. I'd recommend a simple QByteArray which would have the significant advantage of being display independent (unlike a QList<QByteArray> or QVector<>, whatever...) i.e. resizing the view wouldn't involve any mess and horizontal scroll bar can be turned of for nicer display...
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Re: Hex viewer in qt?

    Thanks to both for your reply.

    Actually I have a file in form of a byte which I have read from the HardDisk and then want to show these Bytes.

    If I write these Bytes to a file and view in Hex editor this shows the Hex form.

    But I want this thing in my Qt program when user click the file it shows the Hex value which is in unsigned char array.

  5. #5
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Hex viewer in qt?

    in my code, I am doing same thing, reading a file and displaying in QTextEdit in following way.
    first column displays address, then data in Hex 2rd col ascii value.
    even user can edit this at same position.

    Qt Code:
    1. 1000 0000 AA 65 34 5B 43 66 4B 56 89 D0 E1 .A....B.6.5.3
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Re: Hex viewer in qt?

    Thanks for Reply.

    Ok, I will try your code but want to ask one thing where u pass the file Bytes into the text edit.

    Can this function is enough to do the job or I have to implement some other feature also of text edit.

    Also I want to know whether function like
    getAddressFromPosition
    is inbuild function or they are defined by you.

  7. #7
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Hex viewer in qt?

    m_pProgamWindow->setText(text); //where text is QString and m_pProgamWindow is object of text editor

  8. #8
    Join Date
    Apr 2011
    Location
    Gurgaon
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Hex viewer in qt?

    Hey Rajesh is Text edit sufficient to handle the large amount of data ?
    I mean if the data is of 20 Gb of a single file?

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Hex viewer in qt?

    is Text edit sufficient to handle the large amount of data ? I mean if the data is of 20 Gb of a single file?
    Almost certainly not if you are intending to load the entire file into RAM. You'll need a 64-bit environment, probably 64GB+ of RAM, and lots of coffee for waiting time to even have a chance.

    Loading a small working section, ie.e page at a time, of a file is trivial enough using seek() and read(). Alternatively you can use QFile::map() to make part of a huge file appear in-memory.

Similar Threads

  1. Requirement for versatile image viewer
    By deekayt in forum General Discussion
    Replies: 1
    Last Post: 27th October 2006, 14:28
  2. How can i build a source viewer for debugger
    By blobgrinder in forum Qt Programming
    Replies: 3
    Last Post: 24th October 2006, 11:50
  3. ".hlp" viewer
    By boss_bhat in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2006, 14:44
  4. jpeg viewer
    By chap19150 in forum Qt Programming
    Replies: 9
    Last Post: 6th June 2006, 11:57
  5. Image Viewer
    By sumsin in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2006, 13:29

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.