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. My first program in QT
Forum Update on Tuesday, May 27th 2025

My first program in QT

Scheduled Pinned Locked Moved General and Desktop
35 Posts 4 Posters 18.2k Views 1 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.
  • P Offline
    P Offline
    p3aul
    wrote on last edited by
    #8

    From what you said, I guess you know the answer, but for your own reasons don't wish to reveal it. I see by observing parts of code in more complicated examples that my syntax is correct. Unfortunately, QT is not standard C++. If it was there wouldn't be this foolishness of Slots and Signals instead they would use Methods and Events as in standard C++. . I'm either going to have to get some answers either by waiting and hoping someone else will pitch in and help me or by creating another thread. I asked a simple question. Why can't I get a simple, complete answer. Is knowledge of QT hoarded and only given out bit by bit? I have a book by the way. It's just not a book on QT
    Thanks,
    Paul

    1 Reply Last reply
    0
    • W Offline
      W Offline
      Wilk
      wrote on last edited by
      #9

      Hello.
      The first, Qt instead of QT.
      The second, I guess you should read something about Qt. It's not the same as C++ builder.
      About your last problems: could you, please, post all code if mainwindow.cpp?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #10

        [quote author="p3aul" date="1337383959"]From what you said, I guess you know the answer, but for your own reasons don't wish to reveal it. I see by observing parts of code in more complicated examples that my syntax is correct.
        Paul[/quote]
        Sorry.

        And Sorry for my poor English.

        As C++ builder is a IDE of C++, and you said that you used to C++ builder, so I wrongly thought that you will be very familiar with C++, but seems you not.

        Qt is a library of C++, it isn't a good idea to deep into the Qt world before familiar with C++. Perhaps “C++ Primer” is a good book to start with.

        bq. Unfortunately, QT is not standard C++. If it was there wouldn’t be this foolishness of Slots and Signals instead they would use Methods and Events as in standard C++.

        It depends.

        All the “non-standard” sources will be pre-processed by moc, uic, rcc, and then all you get are C++ “standard” files.

        No matter what happened, your Qt application will be compiled by a “standard” C++ compiler such as GCC, MSVC, Intel-C++, Clang, ..., even C++ builder.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3aul
          wrote on last edited by
          #11

          Main Window .cpp
          @#include "mainwindow.h"
          #include "ui_mainwindow.h"

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);
          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }

          void MainWindow::on_pushButton_clicked()
          {
          lineEdit->setText("Hello");
          }
          @

          Errors:
          /home/owner/qt/newattempt-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../newattempt/mainwindow.cpp:-1: In member function 'void MainWindow::on_pushButton_clicked()':
          /home/owner/qt/newattempt-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Release/../newattempt/mainwindow.cpp:18: error: 'lineEdit' was not declared in this scope

          This error about lineEdit seems to indicate that I need to declare it somewhere in mainwindow.cpp I don't know if this just the tip of the iceberg though. Once that is corrected others may show up. :(

          1 Reply Last reply
          0
          • W Offline
            W Offline
            Wilk
            wrote on last edited by
            #12

            Well, the simpliest solution is to declare a signal like sendText (QString) and connecting it with slot of your QLineEdit like setText, and then emit it in your on_pushButton_clicked () slot.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              p3aul
              wrote on last edited by
              #13

              Robot Herder: I have an excellent book that came with C++ builder version 3. The syntax is correct. In Builder I didn't need to add declarations of Components(Widgets) because Builder added them automatically.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dbzhang800
                wrote on last edited by
                #14

                [quote author="Wilk" date="1337390529"]Well, the simpliest solution is to declare a signal like sendText (QString) and connecting it with slot of your QLineEdit like setText, and then emit it in your on_pushButton_clicked () slot.[/quote]
                It's an answer, but not the simpliest ;-) .

                The right answer have been given by VanDerSam. And I try to given reasons, but failed ;-(

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dbzhang800
                  wrote on last edited by
                  #15

                  [quote author="p3aul" date="1337390576"]Robot Herder: I have an excellent book that came with C++ builder version 3. The syntax is correct. In Builder I didn't need to add declarations of Components(Widgets) because Builder added them automatically.[/quote]

                  If I am right, you never try to see the code behind the drag and drop operation when using C++ builder.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    p3aul
                    wrote on last edited by
                    #16

                    Wilk:
                    @Well, the simpliest solution is to declare a signal like sendText (QString) and connecting it with slot of your QLineEdit like setText, and then emit it in your on_pushButton_clicked () slot.@
                    That's a perfect example of what I'm talking about. What you wrote is NOT C++ it's Qt! I need a book on Qt, not C++!

                    If that's the simplest solution than I need to go back to windows and CodeGear or Visual C++

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      Wilk
                      wrote on last edited by
                      #17

                      The fact is that VanDerSam's solution makes your program dependent on UI and actual names of it's components, while you can change UI itself and/or it elements and/or names of UI elements. While my solution makes your application dependent only on interface of class.

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        p3aul
                        wrote on last edited by
                        #18

                        Robot Hearder:
                        @If I am right, you never try to see the code behind the drag and drop operation when using C++ builder.@

                        Well yes, except for the code snippets you write your self. Let's face it, I'm no purist! I just want to get the job done, and Qt, like C++ builder is a RAD tool. Otherwise you would all just use a text editor and code it all for the fun of it.

                        1 Reply Last reply
                        0
                        • W Offline
                          W Offline
                          Wilk
                          wrote on last edited by
                          #19

                          [quote author="p3aul" date="1337391158"]Wilk:
                          @Well, the simpliest solution is to declare a signal like sendText (QString) and connecting it with slot of your QLineEdit like setText, and then emit it in your on_pushButton_clicked () slot.@
                          That's a perfect example of what I'm talking about. What you wrote is NOT C++ it's Qt! I need a book on Qt, not C++!

                          If that's the simplest solution than I need to go back to windows and CodeGear or Visual C++ [/quote]

                          You're free do do what you want, but Qt is better at least because it's cross platform. If you don't want to learn something new it totally your problem.
                          P.S. Use "quote" function instead of code blocks highlighting for quotes.

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            dbzhang800
                            wrote on last edited by
                            #20

                            ;-) Seems you don't know which problem encountered and complained by p3aul.

                            What he need is a C++ book, no matter which GUI library he want to use.

                            • Qt
                            • MFC
                            • wxWidgets
                            • ...

                            And which C++ IDE he want to selected

                            • Visual Studio
                            • C++ builder
                            • Ellipse
                            • Qt Creator
                            • ...

                            And which C++ compiler he want to choose

                            • cl.exe (MSVC)
                            • g++.exe
                            • clang.exe
                            • ....

                            [quote author="Wilk" date="1337391395"]The fact is that VanDerSam's solution makes your program dependent on UI and actual names of it's components, while you can change UI itself and/or it elements and/or names of UI elements. While my solution makes your application dependent only on interface of class.[/quote]

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              p3aul
                              wrote on last edited by
                              #21

                              I'm using the code format rather than the quote format because that's the only that I can figure out hon this forum. I guess I need a book on Qt forums also? Every one insists that i don't know C++ while I have proven that my syntax is correct. It's just a dead horse you're trying to beat. I know enough to know that I'll will never get an answer here because you are all die-hard loyalists to Qt who can't even accept its deficiencies. Well i guess this post will get me thrown off the forum, but it's just as well. Qt being what it is, i guess ya'll just love java!

                              Robot Hearder:
                              @If I am right, you never try to see the code behind the drag and drop operation when using C++ builder.@

                              Well yes, except for the code snippets you write your self. Let's face it, I'm no purist! I just want to get the job done, and Qt, like C++ builder is a RAD tool. Otherwise you would all just use a text editor and code it all for the fun of it.

                              @The right answer have been given by VanDerSam. And I try to given reasons, but failed ;-(@

                              VanDerSam suggested that I use:

                              ui->lineEdit->setText(“Hello”); and I tried it and came up with about 13 errors. I know I copied and pasted it to replace the code I had written so I know that I used his syntax. Now what is it about that line that makes it the best solution?
                              Thanks,
                              Paul

                              1 Reply Last reply
                              0
                              • W Offline
                                W Offline
                                Wilk
                                wrote on last edited by
                                #22

                                @1+1=2
                                If p3aul needs some book about C++ why than he started a thread here?
                                @p3aul
                                You've mentioned that events are from standard C++... AFAIK they are not. Furthermore, if you think that signals and slots are foolishness, then you just don't understand the idea of them.
                                And I didn't tell that you don't know C++.

                                1 Reply Last reply
                                0
                                • P Offline
                                  P Offline
                                  p3aul
                                  wrote on last edited by
                                  #23

                                  Wilk:
                                  Mia culpa! you are right Events are a MS windows thing. C++ in the windows environment just makes use of them. Methods though are C++ (and Pascal/delphi which calls them functions) I guess I can sorta see why Qt would choose Slots and Signals I just wish they had chosen something a little more descriptive and a little less arcane! ;)

                                  Well it's not like I'm trying for a career as a professional programmer. I'm 70 years old and nobody would hire me anyway! But I used to Program! I learned on an NCR with 64K of memory. A lot back then!
                                  I guess I'll stick with web programming Since the scripts are easier and I can even use them on my Linux machine, I guess I'll stick with that.

                                  1 Reply Last reply
                                  0
                                  • W Offline
                                    W Offline
                                    Wilk
                                    wrote on last edited by
                                    #24

                                    In fact there is no arcane in signal/slots. They are used to make dynamic connections between objects. Also events are still used for processing of keyboard and mouse.

                                    1 Reply Last reply
                                    0
                                    • P Offline
                                      P Offline
                                      p3aul
                                      wrote on last edited by
                                      #25

                                      Then why did I have to use Signals and slots to interact with the mouse and push the button to get text into a textbox?

                                      1 Reply Last reply
                                      0
                                      • W Offline
                                        W Offline
                                        Wilk
                                        wrote on last edited by
                                        #26

                                        AFAIK you use signals not to interact with mouse, but to interact with button. Button has it's own method for processing mouse events. When you push button, it processes this event and emits signal.
                                        You have to use signals to get text into textbox because it's a more flexible way. As I've mentioned, you may totally change your UI, for example use text edit area instead of line edit. You may also use UI, created with desigber separate from your app, and then you will still be able to work with it just thanks to signal/slots.
                                        Furthermore, in you example you may also try
                                        @
                                        #include "mainwindow.h"
                                        #include "ui_mainwindow.h"

                                        MainWindow::MainWindow(QWidget *parent) :
                                            QMainWindow(parent),
                                            ui(new Ui::MainWindow)
                                        {
                                            ui->setupUi(this);
                                        }
                                         
                                        MainWindow::~MainWindow()
                                        {
                                            delete ui;
                                        }
                                         
                                        void MainWindow::on_pushButton_clicked()
                                        {
                                            QLineEdit *lineEdit = findChild <QLineEdit *> ();
                                            if (lineEdit) {
                                              lineEdit->setText("Hello");
                                            }
                                        }
                                        

                                        @
                                        In this code you ask, if main window has line edit element, and if it does, you set it's text.

                                        1 Reply Last reply
                                        0
                                        • P Offline
                                          P Offline
                                          p3aul
                                          wrote on last edited by
                                          #27

                                          It worked! A little more code than the program put in there, but it worked. Now, I ask; If that was the code needed to connect the button to the lineedit, why didn't Qt just put it there in the first place? Just to make me learn more about Qt and C++?

                                          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