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. Stylesheets for widgets on a Dynamic GUI
QtWS25 Last Chance

Stylesheets for widgets on a Dynamic GUI

Scheduled Pinned Locked Moved Solved General and Desktop
texteditline editwidgetsdynamic guistylesheet
12 Posts 5 Posters 4.9k Views
  • 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.
  • M Offline
    M Offline
    mostefa
    wrote on 9 Feb 2017, 18:34 last edited by mostefa 2 Sept 2017, 18:37
    #2

    Hi @Sh1gs , i think that you have to reimplement your paintEvent function, of your dialog class with the following code:

    void YourDialogClass::paintEvent(QPaintEvent *ev)
    {
        QStyleOption o;
        o.initFrom(this);
        QPainter p(this);
        style()->drawPrimitive(
          QStyle::PE_Widget, &o, &p, this);
    }
    

    this is usually used to force custom class to support stylesheet,keep me informed if it works

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sh1gs
      wrote on 9 Feb 2017, 19:20 last edited by
      #3

      Hello @mostefa, thank you for your quick reply. For further information, I am doing something very similar to this QT Example: Config Dialog. I create my main Dialog screen similar to that of the configdialog.cpp style. I am adding my widgets on a "page" like that of the pages.cpp file. Is it here that I would reimplement the paintEvent? Also, how would I handle checking whether the widget has the focus or not?

      For instance, when the application runs, I see all of my widgets on the screen. When I click on the QTextEdit box, it should now have the "focus" and do the logic like changing the background to white. Is this something I also need to add in the paintEvent? Like:

      if(tstTextEdit->hasFocus() == true)
      {
          tstTextEdit->setStyleSheet("QTextEdit:Focus{background:white};");
      }
      

      I apologize if I am not explaining things more clearly, I am confused with how and where to implement the paint function.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mostefa
        wrote on 9 Feb 2017, 19:49 last edited by mostefa 2 Sept 2017, 19:49
        #4

        @Sh1gs Yes you have to add paintEvent on your pages class

        and add it just like that:

        on your pages.h

        void paintEvent(QPaintEvent *ev);
        

        on your pages.cpp

        void pages::paintEvent(QPaintEvent *ev);
        {
            QStyleOption o;
            o.initFrom(this);
            QPainter p(this);
            style()->drawPrimitive(
              QStyle::PE_Widget, &o, &p, this);
        }
        

        and test !

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 9 Feb 2017, 21:54 last edited by
          #5

          Hi,

          Why set that part of the style sheet when the textedit gets the focus ? Set it directly when you created the widget. Focus in that context is a pseudo state that should be detected for you so there's no need for you to do it like that.

          @mostefa there's no need to reimplement the paintEven unless you do something special no supported by the style.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply 10 Feb 2017, 16:48
          2
          • S SGaist
            9 Feb 2017, 21:54

            Hi,

            Why set that part of the style sheet when the textedit gets the focus ? Set it directly when you created the widget. Focus in that context is a pseudo state that should be detected for you so there's no need for you to do it like that.

            @mostefa there's no need to reimplement the paintEven unless you do something special no supported by the style.

            S Offline
            S Offline
            Sh1gs
            wrote on 10 Feb 2017, 16:48 last edited by
            #6

            Hi @SGaist

            Since I have multiple widgets on my application, I don't want the TextEdit to have automatic focus. I am building a simple application for the time being to see how the different widgets react to their own stylesheet as well as a parent stylesheet because that methodology will be used in my actual work project.

            My actual project will have multiple textedits, lineedits, labels, etc on one page, so I need to figure out how to go about checking if one of the textedits has the focus, then to change the background on it accordingly. I unfortunately cannot go into much further detail than that, since that's the nature of my job.

            This brings me to why I am still confused as to how to go about implementing it. From things that I've read in other forums, I assume that I would need something like a mousePressEvent or something similar?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mostefa
              wrote on 10 Feb 2017, 18:22 last edited by
              #7

              @Sh1gs

              We are missing of something through your explanation, i tried an example which work for me without any problem, this is my sample code:

              I have a mainWindow that containt QTextEdit named textEdit,

              code of MainWindow.cpp

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              
              //MainWindow constructor
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
                  //Here define stylesheet for textEdit
              
              setStyleSheet("QTextEdit{background: transparent}/** When we have QTextEdit , background will be transparent**/"
                                "QTextEdit:focus{background: yellow} /** When QTextEdit is focused, background will be white**/ ");
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              

              Hope this can help you, if no , i am waiting for more informations!

              S 1 Reply Last reply 10 Feb 2017, 21:15
              0
              • M mostefa
                10 Feb 2017, 18:22

                @Sh1gs

                We are missing of something through your explanation, i tried an example which work for me without any problem, this is my sample code:

                I have a mainWindow that containt QTextEdit named textEdit,

                code of MainWindow.cpp

                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                
                
                //MainWindow constructor
                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                    //Here define stylesheet for textEdit
                
                setStyleSheet("QTextEdit{background: transparent}/** When we have QTextEdit , background will be transparent**/"
                                  "QTextEdit:focus{background: yellow} /** When QTextEdit is focused, background will be white**/ ");
                }
                
                MainWindow::~MainWindow()
                {
                    delete ui;
                }
                

                Hope this can help you, if no , i am waiting for more informations!

                S Offline
                S Offline
                Sh1gs
                wrote on 10 Feb 2017, 21:15 last edited by
                #8

                @mostefa

                This works if you're using the designer to add the widget, I am not doing it this way. I am creating my widgets at runtime, so more logic is needed in order to do what I want to do. As I've stated before, because of the nature of my job, I cannot explain more than I already have. I appreciate your help, but I will continue to research this.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 10 Feb 2017, 21:19 last edited by
                  #9

                  There's no automatic focus here. That stylesheet pseudo state will be activated when the widget gets the focus. Whether you clicked on it or did it programmatically doesn't matter so you don't have to do any special manipulation. Just like the example of @mostefa is showing.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 10 Feb 2017, 21:22 last edited by mrjj 2 Oct 2017, 21:22
                    #10

                    Hi
                    If you make your stylesheets correctly, they are cascading so they work for
                    dynamically added widgets as well.
                    So if the PARENT of the widget you insert have a stylesheet targeting the
                    type of the child widget, it will automatically be affected.

                    So any child of MainWindow that is a QTextEdit will be affected following @mostefa sample.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Sh1gs
                      wrote on 13 Feb 2017, 18:56 last edited by
                      #11

                      Unfortunately this does not work for me. However, I tried using an eventFilter and that worked the way I wanted it to. I appreciate everyone's input and will now mark this as solved.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        racerXali
                        wrote on 28 Feb 2017, 13:23 last edited by
                        #12

                        Try this , where you create ur control "dynamically"

                        QLineEdit * m_edit=new QLineEdit(this);
                        m_edit->setStyleSheet(":focus{background:white}"); /*this should set the stylesheet*/
                        
                        1 Reply Last reply
                        0

                        • Login

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