PDA

View Full Version : Hex viewer in qt?



vishal.chauhan
16th July 2007, 09:24
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.

rajesh
16th July 2007, 10:57
you can make hexviewer by using text edit event filter. I am send you code of hex viewer from my project.


bool ProgramMemoryWindow::eventFilter(QObject *target, QEvent *event)
{
if (target == m_pProgamWindow)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

if ((keyEvent->key() == Qt::Key_unknown)||(keyEvent->key() == 0))
{
return true;
}

QString str1 = keyEvent->text();
if (str1)
{

char ch = *(str1.ascii());
bool res = true;
if (((ch <= '9' && ch >= '0') | (ch <= 'F' && ch >= 'A') |
(ch <= 'f' && ch >= 'a')))
{
int para,index;
static char chcnt=0;
chcnt++;
m_pProgamWindow->getCursorPosition(&para,&index);
if ((index % 3 == 0))
{
m_pProgamWindow->setCursorPosition(para,index+1);
chcnt = 0;
}
if ((index == 54) && (para == 15))
{
return true;
}
m_pProgamWindow->getCursorPosition(&para,&index);
char value;
getAddressFromPosition(para,index,ch,&value);
char str[4];
if ((value<0x7f)&&(value>0x1F))
str[0]= value;
else
str[0] = 0x2e;
str[1]= '\0';
unsigned int asciipos = ((index - 7)/3)+55;
m_pProgamWindow->setSelection(para,asciipos,para,asciipos+1,0);
m_pProgamWindow->removeSelectedText(0);
m_pProgamWindow->setColor(QColor(255,0,0));
m_pProgamWindow->insertAt(str,para,asciipos);
m_pProgamWindow->selectAll(false);
m_pProgamWindow->setCursorPosition(para,index);
m_pProgamWindow->setSelection(para,index,para,index+1,0);
m_pProgamWindow->setColor(QColor(255,0,0));
return QWidget::eventFilter(target, event);
}
}
if ((keyEvent->key() == Qt::Key_Left )||(keyEvent->key() == Qt::Key_Up )||
(keyEvent->key()==Qt::Key_Right )||(keyEvent->key()==Qt::Key_Down ))
{
return QWidget::eventFilter(target, event);
}
if ((keyEvent->key()==Qt::Key_Enter)||(keyEvent->key()==Qt::Key_Return))
{
saveContents();
return true;
}
if ((keyEvent->key()==Qt::Key_Escape))
{
revertData();
return true;
}
if ((keyEvent->key()==Qt::Key_PageDown))
{
onDownScrollClicked();
return true;
}
if ((keyEvent->key()==Qt::Key_PageUp))
{
onUpScrollClicked();
return true;
}
return true;
}
if ((event->type()== QEvent::Wheel))
{
return true;
}
if(!((event->type()== 10)||(event->type()== 11)||(event->type()== 24)||(event->type()== 25)||(event->type()== 12)))
return QWidget::eventFilter(target, event);

}

return QWidget::eventFilter(target, event);

}

fullmetalcoder
16th July 2007, 11:11
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...

vishal.chauhan
16th July 2007, 15:08
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.

rajesh
16th July 2007, 15:16
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.



1000 0000 AA 65 34 5B 43 66 4B 56 89 D0 E1 .A....B.6.5.3

vishal.chauhan
17th July 2007, 05:32
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.

rajesh
17th July 2007, 06:50
m_pProgamWindow->setText(text); //where text is QString and m_pProgamWindow is object of text editor

anupam
21st June 2013, 06:04
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?

ChrisW67
21st June 2013, 08:58
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.