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. [Solved]Custom Widget?
Forum Updated to NodeBB v4.3 + New Features

[Solved]Custom Widget?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.8k 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.
  • W Offline
    W Offline
    weblife
    wrote on last edited by
    #1

    Hello,

    How do I create a simple widget that would contain a first, middle and last name field and add it to the main window class?

    I ask because I cant figure out why is this simple widget attempt below is not working, what have I missed?

    main
    @int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
    

    }@

    mainWindow class
    @MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
    QMainWindow *mainView = new QMainWindow;
    setCentralWidget(mainView);

    CardUI *card = new CardUI;
    QHBoxLayout *hCard = new QHBoxLayout;
    
    hCard->addWidget(card);
    
    mainView->setLayout(hCard);
    mainView->show();
    

    }@

    cardui class
    @CardUI::CardUI(QWidget *parent) : QWidget(parent){
    QLineEdit *fnText = new QLineEdit;
    QLineEdit *miText = new QLineEdit;
    QLineEdit *lnText = new QLineEdit;

    QHBoxLayout *name = new QHBoxLayout;
    
    name->addWidget(fnText);
    name->addWidget(miText);
    name->addWidget(lnText);
    
    setLayout(name);
    

    }@

    Brandon Clark
    www.themindspot.com

    1 Reply Last reply
    0
    • W Offline
      W Offline
      weblife
      wrote on last edited by
      #2

      Maybe since I actually set CardUI to the layout I should extend the QLayout class?

      Brandon Clark
      www.themindspot.com

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

        You're adding a QMainWindow as the central widget of a QMainWindow.

        Try this:
        @
        QWidget *mainView = new QWidget;
        setCentralWidget(mainView);
        @

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          weblife
          wrote on last edited by
          #4

          From "Sack Overflow":http://stackoverflow.com/questions/11872210/custom-qt-widget/11872467#11872467:

          bq. You should not change layout of QMainWindow. Use setCentralWidget or add toolbars/docks using given API instead. In this particular case you shouldn't create mainView as QMainWindow (you cannot have two main windows in one application, right?). You can change mainView type to QWidget, but you can even don't create any proxy widgets, and just
          @MainWindow::MainWindow(QWidget *parent); : QMainWindow(parent){
          card = new CardUI;
          setCentralWidget(card);
          }@

          Brandon Clark
          www.themindspot.com

          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