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. Qcustomplot without relying on the ui
Forum Updated to NodeBB v4.3 + New Features

Qcustomplot without relying on the ui

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 896 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.
  • L Linz
    ui->QCustomPlot->setStyleSheet(("background:hsva(200, 255, 255, 0%);"));
    ui->plot->setBackground(QBrush(Qt::NoBrush));
    ui->plot->xAxis->setLabel("time");
    ui->plot->yAxis->setLabel("phases");
    ui->plot->xAxis->setRange(-1, 1);
    ui->plot->yAxis->setRange(0, 360);
    ui->plot->replot();
    

    Hello Qt Forum,

    I'm currently struggling to code without relying on Qt Creator-specific features, like the ui form.
    Is there a way to program it? My graph (widget) is realized with qcustomplot.

    You see the example above, currently it is implemented in my main window.cpp, but the ui isn't separated (https://www.qcustomplot.com/index.php/introduction).

    Hopefully you can help me, thanks in advance!

    Linz

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Linz said in Qcustomplot without relying on the ui:

    Hopefully you can help me

    With what exactly?
    It is not clear what problem you have or what the question actually is.

    You, of course, can write UI purely in code without using QtDesigner.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    3
    • L Offline
      L Offline
      Linz
      wrote on last edited by
      #3

      @jsulm
      How it looks right now:

      Bildschirmfoto 2021-06-28 um 13.47.33.png

      I have to write It in a different class without relying on my ui. So the "ui" has to get eliminated.

      jsulmJ 1 Reply Last reply
      0
      • L Linz

        @jsulm
        How it looks right now:

        Bildschirmfoto 2021-06-28 um 13.47.33.png

        I have to write It in a different class without relying on my ui. So the "ui" has to get eliminated.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Linz said in Qcustomplot without relying on the ui:

        So the "ui" has to get eliminated.

        Yes. Instead you put all your widgets/layouts as class members:

        class MyClass...
        {
        private:
            QCustomPlot *plot;
        }
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • L Offline
          L Offline
          Linz
          wrote on last edited by Linz
          #5

          Thanks I tried it, but unfortunately my program is now crashing and I don't know why.
          What did I wrong?

          Graph.h
          Bildschirmfoto 2021-06-28 um 15.24.13.png
          Graph.cpp
          Bildschirmfoto 2021-06-28 um 15.24.23.png

          Mainwindow.cpp
          Bildschirmfoto 2021-06-28 um 15.24.46.png

          J.HilkJ 1 Reply Last reply
          0
          • L Linz

            Thanks I tried it, but unfortunately my program is now crashing and I don't know why.
            What did I wrong?

            Graph.h
            Bildschirmfoto 2021-06-28 um 15.24.13.png
            Graph.cpp
            Bildschirmfoto 2021-06-28 um 15.24.23.png

            Mainwindow.cpp
            Bildschirmfoto 2021-06-28 um 15.24.46.png

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #6

            @Linz
            you forgot to initialise the object with new QCustomPlot()

            to prevent the follow up error, make sure to call show() on it and resize it to a reasonable size

            to prevent the follow up error from that, make yourself familiar with QLayouts:
            https://doc.qt.io/qt-5/qlayout.html


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            L 1 Reply Last reply
            4
            • J.HilkJ J.Hilk

              @Linz
              you forgot to initialise the object with new QCustomPlot()

              to prevent the follow up error, make sure to call show() on it and resize it to a reasonable size

              to prevent the follow up error from that, make yourself familiar with QLayouts:
              https://doc.qt.io/qt-5/qlayout.html

              L Offline
              L Offline
              Linz
              wrote on last edited by
              #7

              @J-Hilk
              I tried to initialise the object in the header with QCustomPlot *plot = new QCustomPlot().
              I get no error but it doesn't do anything :/

              1 Reply Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                C++ basics missing. How long does this object live?:

                MainWindow::MainWindow()
                {
                ...
                  Diagram obj2;
                  obj2.makelot();
                }
                

                And you should add your plot to a layout when you don't want to use a ui file for whatever reason.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                L 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  C++ basics missing. How long does this object live?:

                  MainWindow::MainWindow()
                  {
                  ...
                    Diagram obj2;
                    obj2.makelot();
                  }
                  

                  And you should add your plot to a layout when you don't want to use a ui file for whatever reason.

                  L Offline
                  L Offline
                  Linz
                  wrote on last edited by
                  #9

                  @Christian-Ehrlicher To be honest I'm probably missing some basics. It should be living until its getting called by the destructor. But when exactly this is I do not know.

                  The prof wanted me to get it seperated. Kinda hurts to change a running project... Logic and GUI shouldn't rely on each other.

                  mrjjM 1 Reply Last reply
                  0
                  • L Linz

                    @Christian-Ehrlicher To be honest I'm probably missing some basics. It should be living until its getting called by the destructor. But when exactly this is I do not know.

                    The prof wanted me to get it seperated. Kinda hurts to change a running project... Logic and GUI shouldn't rely on each other.

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

                    Hi
                    I understand what the "prof" says however, truth be told.
                    When you use a UI file. the elements you "draw" on a the form,
                    it converted to code. It's run inside the ui->setup(this).

                    So getting rid of the UI, ONLY moved the code from setupUI struct (the ui )
                    to the class. So the benefit of moving the code into a Diagram class is less obvious.

                    However, it's good to not have all code in MainWindow, and in that regards
                    it's useful advice.

                    To get the last mile, you need to manually insert Diagram into the main window.
                    This must be done by
                    1: new it. not make local variable

                    MainWindow::MainWindow()
                    {
                    ...
                      Diagram obj2; // local variable to the constructor. will die very fast
                      obj2.makelot();
                    }
                    should be like
                    MainWindow::MainWindow()
                    {
                    ...
                      Diagram  *obj2= new   Diagram ; // ponter variable 
                    // 2 new a layout
                    // 3 insert obj2 to the layout
                    // 4 assign layout to MainWindows centralWidget()  
                    }
                    
                    
                    1 Reply Last reply
                    3
                    • S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #11

                      First of all, you should know about Qt's object system. Usually, you will pass a parent object to the constructor of you Qt classes (i.e. QCustomPlot in your case). This means that the parent object will take care of destructing your instance of QCustomPlot. This also means that QCustomPlot needs to be a pointer and that you yourself don't delete it (as it is already taken care of by Qt).

                      I personally don't see a point in creating a Diagramm class which just has a single method which even more so could also be static (if it weren't for the member variable). I would rather make plot a variable of MainWindow and makePlot() a member function of MainWindow. Then it is easy to create the QCustomPlot with MainWindow as its parent. You could also make it a free standing function. If you really have to use the Diagramm class I would have it derive at least from QObject so that Qt can handle its lifetime also. It should then also have MainWindow as its parent.

                      The most important thing is that you put the plot into an existing widget. Otherwise you will most likely not see anything. If MainWindow is the parent it might show up somewhere inside of it (but might also be occluded by something else inside the ui you still have. It is best to put the plot inside an existing layout.

                      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