0

Well this proves I'm a noob at coding! I've looked everywhere and still can't get this right. This should be pretty simple.

I have been trying this:

int i;
for(i=0;i<16;i++)
{
  QChar q = QChar(memblock[i]);
  QString s = QString(q);
  QTableWidgetItem *item = new QTableWidgetItem(s);
  ui->tableWidget->setItem(rowCount, colCount, item);
}

So I'm making table items from each string. That's because I also couldn't figure out how to make table items from just a QChar, or plain char.

But each cell ends up with:

Ý

Whereas when I add in:

cout << memblock[i];

It properly shows:

RIFFd2  WAVEfmt 

Here is the code that reads in the raw data:

ifstream file (text, ios::in|ios::binary|ios::ate);
if (file.is_open())
{
    size = file.tellg();
    memblock = new char [size];
    file.seekg(0, ios::beg);
    file.read(memblock, size);
    file.close();
}

Also, when I do cout << &memblock[i], for i from 0 to 4 I get: RIFFd2 IFFd2 FFd2 Fd2

And so on.

krb686
  • 1,726
  • 9
  • 26
  • 46
  • Could you add to the post the code which initializes the `memblock` variable – Ivan Jun 19 '13 at 16:11
  • `memblock = new char[size]` – krb686 Jun 19 '13 at 16:16
  • Honestly, it should be as simple as `QTableWidgetItem item = new QTableWidgetItem(memblock[i]);` – krb686 Jun 19 '13 at 16:20
  • it may hardly be shorter than this - `QTableWidgetItem *item = new QTableWidgetItem(QChar(memblock[i]));` – Ivan Jun 19 '13 at 16:44
  • @Ivan Unfortunately that results in every cell showing `Ý` – krb686 Jun 19 '13 at 16:50
  • may be you should use an array of `QString` or `QChar` instead of raw chars – Ivan Jun 19 '13 at 16:54
  • I added the line `qDebug() << item` , which puts out: `0x5a4fe20 0x5a50130 0x5a50210 0x5d2bb40 0x5d2b4b0 0x5d2ba60 0x5d2b750 0x5c76b80 0x5c76e20 0x5c76bf0 0x5c76950 0x5c76b10 0x5c76cd0 0x6f425e0 0x6f42810 0x6f42650 ` Looks like the memory address of each char, which would make me think I could just change it to *memblock[i] to dereference and retrieve the real value, but doing so gives the error : `illegal indirection` – krb686 Jun 19 '13 at 16:56
  • I thought about doing that, but I decided on using Qt only for GUI aspects since I have almost no experience/knowledge of c++. One of my main goals is to learn c++. Similar to this guy: http://programmers.stackexchange.com/questions/195568/qt-c-vs-generic-c-and-stl – krb686 Jun 19 '13 at 17:11

1 Answers1

3

I've managed to use some simplified variant of your code with Qt 4.8.3 and MSVC 2010:

ui.tableWidget->setColumnCount(5);
ui.tableWidget->setRowCount(5);
char *memChunck = new char[25];
for ( int i = 0; i < 25; ++i ) {
    memChunck[i] = i + 65;
}
for ( int i = 0; i < 25; ++i ) {
    QTableWidgetItem *item = new QTableWidgetItem(QString::number( memChunck[i], 16).toUpper() );
    ui.tableWidget->setItem(i / 5, i % 5, item);
}

result view

Ivan
  • 588
  • 5
  • 20
  • Okay that seems fairly similar to what I have. I may have somewhat misstated my intention though. I don't actually want Ascii representation of my data, I want the raw data as a string. So in other words, I want 65, 66, 67, 68, and so on. What I really want is this: http://imgur.com/QtaspOK – krb686 Jun 19 '13 at 17:58
  • @krb686, just changed the code. On updated screenshot I've used data for table from the link you provided. – Ivan Jun 19 '13 at 18:19
  • I changed my code to match yours exactly, but it's not quite there for some reason. This is what it looks like after the change: http://imgur.com/tsJ2e22 I think this may have something to do with my `memblock` I updated my original post showing the code I am using to read in raw data. – krb686 Jun 19 '13 at 18:24
  • @krb686, yeah, it seems to me that the reason is how your `memblock` is initialized. Try to read your input file as text rather than as binary. – Ivan Jun 19 '13 at 18:42
  • I got it working! Your code works perfectly actually. I forgot I pasted the reading data section in from another file that also included `delete[] memblock;` before I started trying to make items from the memblock. doh! – krb686 Jun 19 '13 at 18:56