Reputation: 489
everyone!
I'am trying to writing a custom debugger for my own hardware. A problem I face is that I don't know how to realize the add debug point function. In details, I want to enter (F9) in one line, and tag a red point on the left column. Or, I double click the left column and tag a red point.
I have learned the code Editor Example provided by Qt itself, but I have no idea how to realize my design.
Some tiny example project would be better.
The red point looks like this
Upvotes: 1
Views: 190
Reputation: 244252
For simplicity the red circles will be added when clicking in the right space where the numbers are similar to QtCreator
, the following procedure can be extrapolated to your specific requirement.
The first thing to get is the position of the cursor with respect to the editor, actually get the height or rectangle where the line that we want to find is located, in the case of mousePressEvent
returns the position of the click with respect to the widget that draws the numbers, with this height we can get the position with the following algorithm, then store it if it does not exist or delete it if it was already stored (h is the position relative to the top of the widget):
QTextBlock block = firstVisibleBlock();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
int blockNumber = block.blockNumber();
while (block.isValid()) {
if (block.isVisible()) {
if(h > top && h<bottom){
int index = breakpoints.indexOf(blockNumber);
if(index != -1){
breakpoints.remove(index);
}
else
breakpoints<<blockNumber;
update();
return;
}
}
blockNumber++;
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
}
Then in the method that paints the numbers, in this case lineNumberAreaPaintEvent
, as there we have the blocks and their respective index, it is compared and if one of the stored numbers is painted.
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
//add next lines
if(breakpoints.indexOf(blockNumber) != -1){
painter.setBrush(Qt::red);
painter.drawEllipse(0, top + (fontMetrics().height()-width_circle)/2, width_circle, width_circle);
}
//end lines
painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}
The following image shows the result.
The complete example can be found in the following link.
Upvotes: 2