Results 1 to 9 of 9

Thread: Segmentation Fault When Moving QTextCursor Anchor

  1. #1
    Join Date
    Jun 2021
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Segmentation Fault When Moving QTextCursor Anchor

    First time making a program with a GUI, using c++. I have a QTextBrowser that I want to use to provide the user with a text file of lines that they can select, then insert a new line either before or after the selected line. I'm setting up the line-by-line selection using these:

    Qt Code:
    1. connect(ListWindow, &QTextBrowser::cursorPositionChanged, this, &MainWindow::handleSelect);
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. void MainWindow::handleSelect(){
    2. ListWindow->moveCursor(QTextCursor::StartOfLine); //move cursor and anchor to start of line
    3. ListWindow->moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); //move cursor to end of line without anchor, selecting text between
    4. }
    To copy to clipboard, switch view to plain text mode 

    When I click a line in the browser, I get this:
    "The inferior stopped because it received a signal from the operating system.
    Signal name: SIGSEGV
    Signal meaning: Segmentation fault"

    Each line in handleSelect works on its own, the program only crashes when I run both. What do I need to do to avoid this?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,970
    Thanks
    277
    Thanked 808 Times in 797 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Segmentation Fault When Moving QTextCursor Anchor

    A segmentation fault usually happens when you try to use an uninitialized pointer, a pointer that has been deleted, or a null pointer. Since you don't show any more than a few lines of code, it is impossible to tell where this might be happening. What is "ListWindow" and how do you create, initialize, and use it? Is it still in scope and valid when this slot is called?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2021
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Segmentation Fault When Moving QTextCursor Anchor

    Here's the full code:

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTextBrowser>
    6. #include <QPushButton>
    7. #include <QMouseEvent>
    8. #include <QTextCursor>
    9. #include <QTextDocumentFragment>
    10.  
    11. QT_BEGIN_NAMESPACE
    12. namespace Ui { class MainWindow; }
    13. QT_END_NAMESPACE
    14.  
    15. class MainWindow : public QMainWindow
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. explicit MainWindow(QWidget *parent = nullptr);
    21. ~MainWindow();
    22.  
    23. private slots:
    24. void handleInsert();
    25. void handleSelect();
    26. private:
    27. Ui::MainWindow *ui;
    28.  
    29. QPushButton *ButtonInsert;
    30. QTextBrowser *ListWindow;
    31. };
    32.  
    33. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QApplication>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. MainWindow w;
    10. w.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent)
    6. , ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. ButtonInsert = new QPushButton("Insert", this);
    10. ButtonInsert -> setGeometry(QRect(QPoint(440,90), QSize(100,30)));
    11.  
    12. ListWindow = new QTextBrowser(this);
    13. ListWindow -> setGeometry(QRect(QPoint(50,50), QSize(300,500)));
    14. ListWindow -> setText("This is a test");
    15.  
    16. connect(ListWindow, &QTextBrowser::cursorPositionChanged, this, &MainWindow::handleSelect);
    17. connect(ButtonInsert, &QPushButton::released, this, &MainWindow::handleInsert);
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete ui;
    23. }
    24.  
    25. void MainWindow::handleInsert(){
    26. //test code for learning
    27. ButtonInsert->setText("Inserting...");
    28. ButtonInsert->setText("Inserted");
    29. }
    30.  
    31. void MainWindow::handleSelect(){
    32. ListWindow->moveCursor(QTextCursor::StartOfLine);
    33. ListWindow->moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
    34. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    551
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation Fault When Moving QTextCursor Anchor

    You create an infinite loop since you modify the cursor position inside a slot which gets called when the cursor position changes.

  5. The following user says thank you to ChristianEhrlicher for this useful post:

    PrincessTrevor (7th June 2021)

  6. #5
    Join Date
    Jun 2021
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Segmentation Fault When Moving QTextCursor Anchor

    "I'd considered that, but then why would each of these lines work individually without creating an infinite loop, but not together? "

    ...is what I would've said last night. Just thought about it more, each line moves the cursor to the beginning or end, which calls the function again, but it doesn't move the second time since it's already at its destination. With that being the case, do you have any recommendations for what I could do to achieve this effect? Looking through the signals for TextEdit and Browser I don't see anything that would help me here.

  7. #6
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    551
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation Fault When Moving QTextCursor Anchor

    I don't understand what you're trying to achieve at all. Why first move it to the front, then to the end and all this inside a slot which is called when the cursor position changes? It's just pointless.

  8. #7
    Join Date
    Jun 2021
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Segmentation Fault When Moving QTextCursor Anchor

    Not "pointless" for the use of the project. The goal is to select the line the user clicks on. The intent was that when they click, the cursor moves to that line, setting off the cursor move slot. Then pull the anchor to the beginning of the line and then move the cursor alone to the end of the line, which will select it. Obviously the infinite loop wasn't intended, which is why I'm trying to find a better way to do this, which will likely involve not having this process inside the cursorPositionChanged slot but I don't see another way to do it. Odds are I'm just missing something, never worked with QT before.

  9. #8
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    551
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation Fault When Moving QTextCursor Anchor

    Your idea is fine but which connect to cursorPositionChanged then? I mean - how should a user change the cursor at all then?

  10. #9
    Join Date
    Jun 2021
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Segmentation Fault When Moving QTextCursor Anchor

    The user moves the cursor by clicking. In any case, I fixed the issue by changing handleSelect to this:

    Qt Code:
    1. void MainWindow::handleSelect(){
    2. if (ListWindow->textCursor().atBlockEnd() == false){
    3. ListWindow->moveCursor(QTextCursor::StartOfLine);
    4. ListWindow->moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    Works fine for test cases, should be fine moving forward. Thanks for the help.

Similar Threads

  1. problem while moving QTextCursor
    By Abdeljalil in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2014, 11:45
  2. QPlainTextEdit selecting Text without moving anchor
    By janeljanson in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2012, 12:47
  3. a segmentation fault
    By yaohao@qtcentre in forum Qt Programming
    Replies: 4
    Last Post: 17th March 2011, 09:01
  4. Segmentation Fault
    By jmc in forum Qt Tools
    Replies: 4
    Last Post: 24th February 2010, 20:08
  5. segmentation fault
    By shamik in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2006, 07:33

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.