[Solved]Custom Widget?



  • 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);
    

    }@



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


  • Moderators

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

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



  • 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);
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.