Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How add and paint controls to widget?
Forum Updated to NodeBB v4.3 + New Features

How add and paint controls to widget?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 774 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndrzejB
    wrote on last edited by
    #1

    I have CodeEditor derived from QPlainTextEdit, it has sidebar with line numbers.
    I am trying also add searchBar on top.

    class CodeEditor : public QPlainTextEdit {
    ...
       SearchBar *searchBar;
      void updateSearchBarGeometry();
       void updateSearchBarArea(const QRect &rect, int dy);
    ...
      void searchBarPaintEvent(QPaintEvent *event);
    ...
    }
    CodeEditor::CodeEditor(QString path) : path(std::move(path)),
                            highlighter(new KSyntaxHighlighting::SyntaxHighlighter(document())),
                            searchBar(new SearchBar(this)),
                            sideBar(new CodeEditorSidebar(this)){
    connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateSearchBarArea);
    ....
    void CodeEditor::updateSearchBarGeometry() {
        const auto r = rect();
        searchBar->setGeometry(QRect(r.left(), r.top(), r.width(), 20));
    }
    
    void CodeEditor::updateSearchBarArea(const QRect &rect, int dy)
    {
        if (dy) {
            searchBar->scroll(0, dy);
        } else {
            searchBar->update(0, rect.y(), rect.width(), 20);
        }
    }
    void CodeEditor::searchBarPaintEvent(QPaintEvent *event) {
        QPainter painter(searchBar);
        QRect r = event->rect();
        painter.fillRect(r, Qt::red);
    }
    
    class SearchBar : public QWidget {
        CodeEditor *codeEditor;
    public:
        explicit SearchBar(CodeEditor *editor);
        QSize sizeHint() const override;
    protected:
        void paintEvent(QPaintEvent *event) override;
    };
    #include "SearchBar.h"
    #include "CodeEditor.h"
    
    SearchBar::SearchBar(CodeEditor *editor)
            : QWidget(editor)
            , codeEditor(editor)
    {
    }
    
    QSize SearchBar::sizeHint() const {
        return QSize(0, 20);
    }
    
    void SearchBar::paintEvent(QPaintEvent *event) {
        codeEditor->searchBarPaintEvent(event);
    }
    

    It draws correct width red bar.
    Next, I want add one-line or text control , also add toolbar/coolbar with icons.
    How add? How paint not by QPainter but paint these controls?
    How enable focus on this bar?

    mrjjM 1 Reply Last reply
    0
    • A Offline
      A Offline
      AndrzejB
      wrote on last edited by
      #6

      My solution:
      instead add to tabWidget widget derived from PlainTextEdit, widget will group widgets:

      • QPlainTextEdit
      • sidebar
      • searchbar
         plainEdit = new PlainTextEdit(this);
         sideBar = new CodeEditorSidebar(this);
         auto *hLayout = new QHBoxLayout;
          hLayout->setContentsMargins(0,0,0,0);
          hLayout->setSpacing(0);
          hLayout->addWidget(sideBar);
          hLayout->addWidget(plainEdit);
          setLayout(hLayout);
      

      PlainTextEdit is instead QPlainTextEdit, because sideBar needs methods, which are protected:

      class PlainTextEdit : public QPlainTextEdit {
      public:
          explicit PlainTextEdit(QWidget *parent = nullptr): QPlainTextEdit(parent){};
          QTextBlock firstVisibleBlock_pub() {
              return QPlainTextEdit::firstVisibleBlock();
          }
          QRectF blockBoundingGeometry_pub(const QTextBlock &block) const {
              return QPlainTextEdit::blockBoundingGeometry(block);
          }
          QPointF contentOffset_pub() const {
              return QPlainTextEdit::contentOffset();
          }
          QRectF blockBoundingRect_pub(const QTextBlock &block) const {
              return QPlainTextEdit::blockBoundingRect(block);
          }
          virtual void resizeEvent(QResizeEvent *e) override {
              return QPlainTextEdit::resizeEvent(e);
          }
      };
      
      1 Reply Last reply
      1
      • A AndrzejB

        I have CodeEditor derived from QPlainTextEdit, it has sidebar with line numbers.
        I am trying also add searchBar on top.

        class CodeEditor : public QPlainTextEdit {
        ...
           SearchBar *searchBar;
          void updateSearchBarGeometry();
           void updateSearchBarArea(const QRect &rect, int dy);
        ...
          void searchBarPaintEvent(QPaintEvent *event);
        ...
        }
        CodeEditor::CodeEditor(QString path) : path(std::move(path)),
                                highlighter(new KSyntaxHighlighting::SyntaxHighlighter(document())),
                                searchBar(new SearchBar(this)),
                                sideBar(new CodeEditorSidebar(this)){
        connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateSearchBarArea);
        ....
        void CodeEditor::updateSearchBarGeometry() {
            const auto r = rect();
            searchBar->setGeometry(QRect(r.left(), r.top(), r.width(), 20));
        }
        
        void CodeEditor::updateSearchBarArea(const QRect &rect, int dy)
        {
            if (dy) {
                searchBar->scroll(0, dy);
            } else {
                searchBar->update(0, rect.y(), rect.width(), 20);
            }
        }
        void CodeEditor::searchBarPaintEvent(QPaintEvent *event) {
            QPainter painter(searchBar);
            QRect r = event->rect();
            painter.fillRect(r, Qt::red);
        }
        
        class SearchBar : public QWidget {
            CodeEditor *codeEditor;
        public:
            explicit SearchBar(CodeEditor *editor);
            QSize sizeHint() const override;
        protected:
            void paintEvent(QPaintEvent *event) override;
        };
        #include "SearchBar.h"
        #include "CodeEditor.h"
        
        SearchBar::SearchBar(CodeEditor *editor)
                : QWidget(editor)
                , codeEditor(editor)
        {
        }
        
        QSize SearchBar::sizeHint() const {
            return QSize(0, 20);
        }
        
        void SearchBar::paintEvent(QPaintEvent *event) {
            codeEditor->searchBarPaintEvent(event);
        }
        

        It draws correct width red bar.
        Next, I want add one-line or text control , also add toolbar/coolbar with icons.
        How add? How paint not by QPainter but paint these controls?
        How enable focus on this bar?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @AndrzejB
        Hi
        Since SearchBar is QWidget, you can insert a layout to it and then insert any other widget you wish.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on last edited by
          #3

          First of all, I strongly recommand you to study Qt Widgets - Application Example

          You have to start with QMainWindow as your base class, then you will be able to add a tool bar with a search bar easily.

          From the Qt example:

          MainWindow::MainWindow()
              : textEdit(new QPlainTextEdit)
          {
              setCentralWidget(textEdit);
          

          You have to replace textEdit with codeEditor:

          MainWindow::MainWindow()
              : codeEditor(new CodeEditor)
          {
              setCentralWidget(codeEditor);
          
          1 Reply Last reply
          2
          • A Offline
            A Offline
            AndrzejB
            wrote on last edited by
            #4

            I see Qt/Examples/Qt-5.15.2/uitools/
            how to use
            Qt/5.15.2/Src/qttools/src/shared/findwidget/
            ?

            mrjjM 1 Reply Last reply
            0
            • A AndrzejB

              I see Qt/Examples/Qt-5.15.2/uitools/
              how to use
              Qt/5.15.2/Src/qttools/src/shared/findwidget/
              ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #5

              /qttools/src/shared/findwidget/

              Hi
              The ItemViewFindWidget ?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AndrzejB
                wrote on last edited by
                #6

                My solution:
                instead add to tabWidget widget derived from PlainTextEdit, widget will group widgets:

                • QPlainTextEdit
                • sidebar
                • searchbar
                   plainEdit = new PlainTextEdit(this);
                   sideBar = new CodeEditorSidebar(this);
                   auto *hLayout = new QHBoxLayout;
                    hLayout->setContentsMargins(0,0,0,0);
                    hLayout->setSpacing(0);
                    hLayout->addWidget(sideBar);
                    hLayout->addWidget(plainEdit);
                    setLayout(hLayout);
                

                PlainTextEdit is instead QPlainTextEdit, because sideBar needs methods, which are protected:

                class PlainTextEdit : public QPlainTextEdit {
                public:
                    explicit PlainTextEdit(QWidget *parent = nullptr): QPlainTextEdit(parent){};
                    QTextBlock firstVisibleBlock_pub() {
                        return QPlainTextEdit::firstVisibleBlock();
                    }
                    QRectF blockBoundingGeometry_pub(const QTextBlock &block) const {
                        return QPlainTextEdit::blockBoundingGeometry(block);
                    }
                    QPointF contentOffset_pub() const {
                        return QPlainTextEdit::contentOffset();
                    }
                    QRectF blockBoundingRect_pub(const QTextBlock &block) const {
                        return QPlainTextEdit::blockBoundingRect(block);
                    }
                    virtual void resizeEvent(QResizeEvent *e) override {
                        return QPlainTextEdit::resizeEvent(e);
                    }
                };
                
                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved