0

I am currently writing a simple Hex Viewer application in Qt 5+. I have almost everything together but I am really struggling with one last issue I could use some assistance on.

As It stands I have a main window with 3 subclassed QPlainTextEdit widgets (to handle simultaneous scrolling of all 3) displayed in a row to the user. The first column is simply a line address indexing, second is the Hex view, third is the ASCII translation where Every character that is not in extended ASCII is replaced with a '.' (like most hex viewers). Everything I have is working as expected, but I have no idea where to go with the last feature I need.

When the user highlights Hex in the middle text edit, I want the ASCII that relates to it to be highlighted as well in the right text edit (and vice versa). I have no clear idea how to go about doing this last task.

Could someone please point me in the right direction and help me to figure this aspect out? One additional issue is I need to account for space in the hex view as it displays each line as

XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX

I'm sure that aspect is trivial after the first question is answered.

theB
  • 6,450
  • 1
  • 28
  • 38

2 Answers2

0

It's possible to use a custom cursor for selections. You can use such cursors with a QPlainTextEdit.

QTextCursor *cursor = new QTextCursor(ui->plainTextEdit->document());
cursor->setPosition(StartPos,QTextCursor::MoveAnchor);
cursor->setPosition(EndPos,QTextCursor::KeepAnchor);
cursor->select(QTextCursor::BlockUnderCursor);

See : Qt: create custom QTextCursor select and Selecting a piece of text using QTextCursor

Community
  • 1
  • 1
Aaron
  • 1,181
  • 7
  • 15
  • While this may answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – IKavanagh Oct 22 '15 at 08:15
  • Thankyou for the reply! Since my initial write of the post I have come across this method of text selection.... however where my main struggle remains is how to link together the two custom qtextedit widgets so that when I highlight an area in one view the other other text edit is highlighted accordingly. aka for every 2 hex characters highlighted in the text display 1 char is highlighted in the ascii display. I have the math down for the number of characters to highlight, but the linking between the two is not coming together for me.... any suggestions on this front? – Shootin4Aces Oct 23 '15 at 13:40
0

This is a working Qt4 code example (Qt5 should be the same).

Copies and alters the selection from one textedit to the other.

Just created the project using QtCreator. Added two QPlainTextEdits and added the slots in the header file.

This is the important part of the .cpp file:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTextCursor>

MainWindow::MainWindow(QWidget *parent) :
   QMainWindow(parent),
   ui(new Ui::MainWindow)
{
   ui->setupUi(this);
   connect(ui->textEdit1, SIGNAL(selectionChanged()), SLOT(selectionEdit1Changed()));
   connect(ui->textEdit2, SIGNAL(selectionChanged()), SLOT(selectionEdit2Changed()));

   ui->textEdit1->setPlainText("1234567891234567891234567891234567891234567890");
   ui->textEdit2->setPlainText("1234567890123456789123456789123456789123456789");

   ui->textEdit1->setReadOnly(true);
   ui->textEdit2->setReadOnly(true);
}

void MainWindow::selectionEdit1Changed()
{
   QTextCursor tc = ui->textEdit1->textCursor();
   int newSelectionEnd = tc.selectionEnd()/2;
   if(ui->textEdit2->toPlainText().size() > newSelectionEnd) {

      tc.setPosition(tc.selectionStart(),QTextCursor::MoveAnchor);
      tc.setPosition(newSelectionEnd,QTextCursor::KeepAnchor);

      ui->textEdit2->setTextCursor(tc);
    }
}

void MainWindow::selectionEdit2Changed()
{
   QTextCursor tc = ui->textEdit2->textCursor();
   int newSelectionEnd = tc.selectionEnd()*2;
   if(ui->textEdit1->toPlainText().size() > newSelectionEnd) {

      tc.setPosition(tc.selectionStart(),QTextCursor::MoveAnchor);
      tc.setPosition(newSelectionEnd,QTextCursor::KeepAnchor);

      ui->textEdit1->setTextCursor(tc);
    }
}
Aaron
  • 1,181
  • 7
  • 15
  • Excellent, thankyou! I was on the right track but doing things in a slightly different way.... some tweaks and math to fit my exact needs, but thankyou! – Shootin4Aces Oct 26 '15 at 17:12