Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ QT QT-Creator
Okay, so I'm trying to simply add a custom class of mine as a child of a frame in my main window in Qt:
 
customctrl.h:
 
#ifndef CUSTOMCTRL_H
#define CUSTOMCTRL_H
 
#include <QWidget>

namespace Ui {
class customctrl;
}
 
class customctrl : public QWidget
{
    Q_OBJECT
    
public:
    explicit customctrl(QWidget *parent = 0);
    ~customctrl();
    
private:
    Ui::customctrl *ui;
};
 
#endif // CUSTOMCTRL_H

 
and my main window:
 
#include "customctrl.h"

//A bunch of code...
{
    customctrl *cc = new customctrl();
    ui->frame->layout()->addWidget(cc);
}
 
And yet, every time I run this code, my program crashes and I don't know why. Is there something I'm missing? What am I doing wrong? Please help me! Thank you!!!
Posted 20-Jun-13 17:31pm
Comments
Richard MacCutchan at 21-Jun-13 3:34am
   
Step through the code with the debugger to see exactly what is happening.
RadXPictures at 24-Jun-13 15:18pm
   
When I try debugging I get a "debugger not set up" error. Any idea?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to implement all methods from 'customctrl.h' file in 'customctrl.cpp' file:
 
customctrl.cpp
========================================
#include "customctrl.h"

 
customctrl::customctrl(QWidget *parent = 0) : QWidget(parent) 
{
  resize(50, 50);
  setWindowTitle("Qt");
}
 

customctrl::~customctrl() {;}
=========================================
  Permalink  
v2
Comments
RadXPictures at 23-Jun-13 22:28pm
   
Sorry, probably should have posted it, but I already had:
 
#include "customctrl.h"
#include "ui_customctrl.h"
 
customctrl::customctrl(QWidget *parent) :
QWidget(parent),
ui(new Ui::customctrl)
{
ui->setupUi(this);
}
 
customctrl::~customctrl()
{
delete ui;
}
 
It was auto-generated.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Wow... just figured out an unbelievably simple solution. Thanks anyways guys.
 
   customctrl *cc = new customctrl(ui->frame);
   cc->show();
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 190
1 Jochen Arndt 165
2 PIEBALDconsult 150
3 BillWoodruff 148
4 DamithSL 140
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 24 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100