Qt plainTextEdit jump to line - qt

I wirte a codeEdit based on plainEdit, and I need to move to specified line. The below code relize the function patially. The proplem is that the cursor is on the bottom of the widget. Is there some way to put the cursor (yellow line) in the middle of the widget.
void MainWindow::run(){
QTextCursor text_cursor(SPUEdit->document()->findBlockByNumber(100));
SPUEdit->setTextCursor(text_cursor);
// SPUEdit->verticalScrollBar()->setValue(12);
}

You should call centerCursor method of QPlainTextEdit:
void QPlainTextEdit::centerCursor()
Scrolls the document in order to
center the cursor vertically.

Related

Reestablishing expected behavior of cursor in QTextEdit following a dropEvent

I wrote a class CustomTextEdit inheriting from qTextEdit and overriding the following two methods:
bool canInsertFromMimeData(const QMimeData *source) const override;
void dropEvent(QDropEvent *e) override;
so that it is possible to use the widget as the destination of a drag-and-drop action.
This has unexpected consequences on the behavior of the text cursor in this widget.
Before any dropEvent, it is possible to visually see where the text cursor is placed.
The text cursor position moves to the last place where the mouse has been clicked.
This is the expected behavior of the cursor.
Following the execution of the dropEvent, the vertical bar rendering of the text cursor
remains positioned after the inserted text. The text cursor no longer moves to the position
of the last click in the QTextEdit area.
If other dropEvents are executed, several vertical bars may end up being displayed.
What is the correct procedure to reestablish the default behavior of the text cursor after
execution of a dropEvent?
Even the following definition of the dropEvent method produces this strange behavior:
void CustomTextEdit::dropEvent(QDropEvent *e)
{
this->insertPlainText(QString("<<dropped text>>"));
}
The solution has been to explicitly set the text cursor before inserting the text, and to call the dropEvent method on the parent class QTextEdit.
void CustomTextEdit::dropEvent(QDropEvent *e)
{
// setting the text cursor
QTextCursor cursor = this->textCursor();
cursor.setPosition(cursorForPosition(e->pos()).position());
this->setTextCursor(cursor);
// dropping the text
this->insertPlainText(QString("<<dropped text>>"));
// conclude the call by calling the parent method
QTextEdit::dropEvent(e);
}

How to replace down arrow with text in a combobox in JavaFX

I'm trying to remove the down arrow from a combobox. All the solutions I have found just make the arrow disappear, for example this one.
Is there a way to remove completely the space where the arrow appears and fill the box just with the text of the selected choice?
If you want to completely elimnate the arrow & arrow button space, you can try with the below custom ComboBox.
The below code is setting the arrow button and arrow nodes size to 0 and asking to rerender the comboBox. The null check is to let this changes apply only once.
public class MyComboBox<T> extends ComboBox<T>{
Region arrowBtn ;
#Override
protected void layoutChildren() {
super.layoutChildren();
if(arrowBtn==null){
arrowBtn= (Region)lookup(".arrow-button");
arrowBtn.setMaxSize(0,0);
arrowBtn.setMinSize(0,0);
arrowBtn.setPadding(new Insets(0));
Region arrow= (Region)lookup(".arrow");
arrow.setMaxSize(0,0);
arrow.setMinSize(0,0);
arrow.setPadding(new Insets(0));
// Call again the super method to relayout with the new bounds.
super.layoutChildren();
}
}
}
UPDATE :
Based on the suggestion of #kleopatra, we can get the same behaviour using css as well (without the need to create a new class for ComboBox).
.combo-box .arrow-button,
.combo-box .arrow{
-fx-max-width:0px;
-fx-min-width:0px;
-fx-padding:0px;
}
The below image will tell you the difference of a normal combox box and this custom combo box. The left one is the normal comboBox, you can see the list cell when inspecting with ScenicView. The right one is the custom one. The list cell is completely occupied suppressing the arrow space.

QMenu being displayed outside the main window

I'm using a custom QGraphicsWidget and when I right click on it I want to bring up a menu. I'm starting it like this:
void myQGraphicsWidget::mousePressEvent(QGraphicsSceneMouseEvent *event){
if(event->button() & Qt::RightButton){
const QString test = "test";
QMenu menu;
menu.setTitle(test);
menu.addAction(test);
menu.exec(mapToScene(event->pos()).toPoint());
//menu.exec(mapToScene(QPointF(0,0)).toPoint());
}
}
but the menu shows up way outside of the main application window towards the bottom right of my other monitor. When I use the commented out version then it appears resting on top of my main window. I've tried adjusting the point manually to massage it inside the window but it will just jump to either resting on top of the window or hanging from the bottom, never inside.
QMenu::exec takes a global position; you're taking the widget-relative position and mapping it to the scene position.
Try this instead:
menu.exec(event->screenPos());

Moving object with mouse

I use Qt and I want to move some object with mouse. For example, user clicks on object and drag this object to another place of window. How I can do it?
I tried mouseMoveEvent:
void QDropLabel::mouseMoveEvent(QMouseEvent *ev)
{
this->move(ev->pos());
}
but unfortunately object moves very strange way. It jumps from place to place.
QDropLabel inherits QLabel. Also it has given a pixmap.
I tried to do it with different objects, but result is same.
Your movable widget must have a QPoint offset member. It will store a position of the cursor click relative to the widget's top left corner:
void DropLabel::mousePressEvent(QMouseEvent *event)
{
offset = event->pos();
}
On mouse move event you just move your widget in its parent coordinate system. Note that if you don't subtract offset from the cursor position, your widget will 'jump' so its top left corner will be just under the cursor.
void DropLabel::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
this->move(mapToParent(event->pos() - offset));
}
}

Lower text in a QMessagebox

I have a QMessagebox with a custom background image. Since there is some stuff on the top side of the background image I want to see, the text of the messagebox should be lowered. Does anybody know how I can do this? I already tried throwing in some white lines using br, so:
popup.setText("<font size =5 color =white ><br>""<br>""<br>""Are you sure you
want to erase the memory</font> ");
but this screws up the background picture. Is there any way I can move the "box" that contains the text to a lower position?
You could try to get the QMessageBox' layout, get the label which holds your text and increment the labels margin. This probably is a hack and might make your project unportable. Construct your QMessageBox, call hack and then exec the box.
void hack(QMessageBox* pMessageBox)
{
QGridLayout* grid = qobject_cast<QGridLayout*>(pMessageBox->layout());
if (grid)
{
QLabel* label = qobject_cast<QLabel*>((grid->itemAtPosition(0,1))->widget());
if (label)
{
label->setMargin(label->margin()+5); // whatever is suitable
}
}
}

Resources