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. Pop up examples
QtWS25 Last Chance

Pop up examples

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 37.7k 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.
  • P Offline
    P Offline
    Peppy
    wrote on last edited by
    #4

    It will be better to start with widget (QWidget) as window, MainWindow(QMainWindow) is different and quite harder...

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jk_mk
      wrote on last edited by
      #5

      Yes, you have right. I am using QWidget so far, but I went a little bit confused with the terminoly

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jk_mk
        wrote on last edited by
        #6

        Could somebody give me an advise?
        Thanks

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Peppy
          wrote on last edited by
          #7

          Ah, okay, I thought, you've inherited MainWindow from QWidget not from QMainWindow...okay then...

          This is KISS code, which I can write from my head...

          This is the smallest code:
          @
          #include <QApplication>
          #include <QMessageBox>
          #include <QPushButton>
          #include <QWidget>
          #include <QObject>

          class MainWindow : public QWidget
          {
          Q_OBJECT
          public slots:
          void buttonPressed();
          };

          void MainWindow::buttonPressed()
          {
          QMessageBox::information(0, QString("Information"), QString("You've pressed the button "Press Me!""), QMessageBox::Ok);
          }

          int main(int argc, char* argv[])
          {
          QApplication a(argc,argv);

          MainWindow mW;
          QPushButton* PressMe = new QPushButton("Press me!", &MainWindow);
          QObject::connect(PressMe, SIGNAL(triggered()), mW, SLOT(buttonPressed()));
          mW.show();

          return a.exec();
          }
          @

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jk_mk
            wrote on last edited by
            #8

            Thanks for your reply.And to be more specific I have created the following code which I want to use it as a pop up window when I press a button in an other Widget. What sould I do in Qt visual studio add-in, to compine this application?

            @
            ////////// window.h ///////////
            #ifndef WINDOW_H
            #define WINDOW_H

            #include <QDialog>

            class QComboBox;
            class QDir;
            class QLabel;
            class QPushButton;

            class Window : public QDialog
            {
            Q_OBJECT

            public:
            Window(QWidget *parent = 0);

            private slots:
            void browse();

            private:

             QPushButton *createButton(const QString &text, const char *member);
             QComboBox *createComboBox(const QString &text = QString());
            
            
            
             QComboBox *directoryComboBox; 
             QLabel *directoryLabel;
             QPushButton *browseButton;
            

            QPushButton *openButton;

            };

            #endif
            @

            @
            ////////// window.cpp /////////////
            #include <QtGui>

            #include "window.h"

            Window::Window(QWidget *parent)
            : QDialog(parent)
            {

             browseButton = createButton(tr("&Browse..."), SLOT(browse()));
            

            openButton = createButton(tr("&Open"), SLOT(open()));

             directoryComboBox = createComboBox(QDir::currentPath());
            
             directoryLabel = new QLabel(tr("In directory:"));
            
            
             QHBoxLayout *buttonsLayout = new QHBoxLayout;
             buttonsLayout->addStretch();
            

            buttonsLayout->addWidget(openButton);

             QGridLayout *mainLayout = new QGridLayout;
             mainLayout->addWidget(directoryLabel, 2, 0);
             mainLayout->addWidget(directoryComboBox, 2, 1);
             mainLayout->addWidget(browseButton, 2, 2);
             mainLayout->addLayout(buttonsLayout, 5, 0, 1, 3);
             setLayout(mainLayout);
            
             setWindowTitle(tr("Open Files"));
             resize(500, 200);
            

            }

            void Window::browse()
            {
            QString directory = QFileDialog::getExistingDirectory(this,
            tr("Open Files"), QDir::currentPath());
            if (!directory.isEmpty()) {
            directoryComboBox->addItem(directory);
            directoryComboBox->setCurrentIndex(directoryComboBox->currentIndex() + 1);
            }
            }

            QPushButton *Window::createButton(const QString &text, const char *member)
            {
            QPushButton *button = new QPushButton(text);
            connect(button, SIGNAL(clicked()), this, member);
            return button;
            }

            QComboBox *Window::createComboBox(const QString &text)
            {
            QComboBox *comboBox = new QComboBox;
            comboBox->setEditable(true);
            comboBox->addItem(text);
            comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
            return comboBox;
            }
            @

            @
            /////////// main.cpp //////////
            #include <QApplication>

            #include "window.h"

            int main(int argc, char *argv[])
            {
            QApplication app(argc, argv);
            Window window;
            window.show();
            return app.exec();
            }
            @

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jk_mk
              wrote on last edited by
              #9

              sorry for the previous format:

              @/////////////window.h //////////
              #ifndef WINDOW_H
              #define WINDOW_H

              #include <QDialog>

              class QComboBox;
              class QDir;
              class QLabel;
              class QPushButton;

              class Window : public QDialog
              {
              Q_OBJECT

              public:
              Window(QWidget *parent = 0);

              private slots:
              void browse();

              private:

               QPushButton *createButton(const QString &text, const char *member);
               QComboBox *createComboBox(const QString &text = QString());
              
              
              
               QComboBox *directoryComboBox; 
               QLabel *directoryLabel;
               QPushButton *browseButton;
              

              QPushButton *openButton;

              };

              #endif

              ////// window.cpp/////////
              #include <QtGui>

              #include "window.h"

              Window::Window(QWidget *parent)
              : QDialog(parent)
              {

               browseButton = createButton(tr("&Browse..."), SLOT(browse()));
              

              openButton = createButton(tr("&Open"), SLOT(open()));

               directoryComboBox = createComboBox(QDir::currentPath());
              
               directoryLabel = new QLabel(tr("In directory:"));
              
              
               QHBoxLayout *buttonsLayout = new QHBoxLayout;
               buttonsLayout->addStretch();
              

              buttonsLayout->addWidget(openButton);

               QGridLayout *mainLayout = new QGridLayout;
               mainLayout->addWidget(directoryLabel, 2, 0);
               mainLayout->addWidget(directoryComboBox, 2, 1);
               mainLayout->addWidget(browseButton, 2, 2);
               mainLayout->addLayout(buttonsLayout, 5, 0, 1, 3);
               setLayout(mainLayout);
              
               setWindowTitle(tr("Open Files"));
               resize(500, 200);
              

              }

              void Window::browse()
              {
              QString directory = QFileDialog::getExistingDirectory(this,
              tr("Open Files"), QDir::currentPath());
              if (!directory.isEmpty()) {
              directoryComboBox->addItem(directory);
              directoryComboBox->setCurrentIndex(directoryComboBox->currentIndex() + 1);
              }
              }

              QPushButton *Window::createButton(const QString &text, const char *member)
              {
              QPushButton *button = new QPushButton(text);
              connect(button, SIGNAL(clicked()), this, member);
              return button;
              }

              QComboBox *Window::createComboBox(const QString &text)
              {
              QComboBox *comboBox = new QComboBox;
              comboBox->setEditable(true);
              comboBox->addItem(text);
              comboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
              return comboBox;
              }

              /////////// main.cpp /////////
              #include <QApplication>

              #include "window.h"

              int main(int argc, char *argv[])
              {
              QApplication app(argc, argv);
              Window window;
              window.show();
              return app.exec();
              }@

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #10

                Hi jk_mk,

                next tme, please edit your last post, it's simple:
                under your avatar icon, click on edit :-)

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

                  what you should do? I didn't have VS add-in, so I don't know...

                  And now is here the code twice, good :)

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jk_mk
                    wrote on last edited by
                    #12

                    I have created the this code which I want to use it as a pop up window when I press a button in an other Widget

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #13

                      bq. Thanks for your reply.And to be more specific I have created the following code which I want to use it as a pop up window when I press a button in an other Widget. What sould I do in Qt visual studio add-in, to compine this application?

                      jk_mk, your question is quite unclear to me.
                      Please rephrase it and tell us, what your problem is.

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        tobias.hunger
                        wrote on last edited by
                        #14

                        Note that you do not need to add .cpp files to resources. Resources are for things you want to have available in your application at runtime and do not want to drop onto the filesystem. Stuff needed at compile time (like headers and sources) should never go into the resources.

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          Peppy
                          wrote on last edited by
                          #15

                          (at)Gerolf: He wants to compile it with Visual Studio Add-in, but probably he doesn't know what to press/set-up...
                          (at)jk-mk: I advise you look at function QObject::connect() with this structure:

                          @QObject::connect(sender, signal, reciever, slot);@

                          Better written: "Signals & Slots":http://doc.qt.nokia.com/latest/signalsandslots.html

                          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