0

I'm doing a college project in QT with C++. I basically have it all done, but I kind of want to end of a flourish! At the endgame, you get to the last room and press the control panel and a keypad appears (which is a .ui layout created as a QWidget) - the access code is a randomly generated 4 digit number in an earlier room.

Anyways, I want to pop up the QWidget with the keypad, get the user to press 4 buttons - each button would return a QString - and then press the confirm button. If it matches, game ends. If not, returned to room.

I just have no idea how to call the widget! The API haven't really help as I don't see anyway to assign a .ui form to the QWidget object.

Chris O'Brien
  • 372
  • 6
  • 25

2 Answers2

1

The .ui file is a resource file. If the setup you have does not do this for you automatically, then you must use the uic tool to convert the .ui file to c++ source code.

 foo.ui -> ui_foo.h

This header contains a class that creates the widgets and has members to acces each of the members once they have been created.

class Ui_Foo {
   setupUi(QWidget *) { ...
   }
}

namespace Ui {
    class Foo: public Ui_Foo {};
} // namespace Ui

An instance of Ui::Foo is placed in your FooWidget

// FooWidget.h
//
class FooWidget 
: public QWidget {
    FooWidget(QWidget *);
    Ui::Foo  mUi;
}

and its setupUi is called in the constructor of your FooWidget

// FooWidget.cpp
// 
FooWidget::FooWidget(QWidget *parent)
: QWidget(parent)
{
    mUi.setupUi(this);
}
Thomas
  • 4,980
  • 2
  • 15
  • 30
  • That's a great help. I'll give it a go tomorrow. I think on my initial go, I created it without the source files as I definitely wasn't asked for them. – Chris O'Brien Feb 24 '13 at 19:14
  • I have it kind of working. I included the .h file and created a point QWidget *accessWidget; I then created the object using accessWidget = new QWidget(0,0); Then I tried to show it using accessWidget.show(); I got a window, but it was just a black window, despite the .ui file being created. Do I need to use create() first? – Chris O'Brien Feb 24 '13 at 20:29
  • You have to derive your own AccessWidget from QWidget (see slight edit above). In the constructor of your AccessWidget you call the setupUi method. You then create a `new AccessWidget(0,0)`, not QWidget !! – Thomas Feb 24 '13 at 20:53
  • Right, I'm nearly there. I just need one more thing. I need to compare the string generated in the widget to one in the mainwindow. This is the code If I try TheIslandMain.accessInput = input (where accssInput is a QString declared in TheIslandMain.h and included in accessWidget.h) and input is declared in accessWidget.h, I get the following error. C\accesspanel.cpp:79: error: C2143: syntax error : missing ';' before '.' – Chris O'Brien Feb 24 '13 at 21:54
  • Don't really know enough about your code to answer that, but `TheIslandMain` seems to be the name of a type and not of a variable. If accessInput is a public static member of the it can be accessed as `TheIslandMain::accessInput`. Other wise you must write something like `foo->accessInput` or `foo.accessInput` where foo is is a pointer to, reference to or instance of the `TheIslandMain`. – Thomas Feb 24 '13 at 23:27
  • TheIslandMain is the main ui object creates by the QT Creator. I'm coming from a java world so I keep on defaulting to a . I tried TheIslandMain::accessInput (which is a ref to a public QString, bad practice, I know!), but I already tried it with methods. Initially, I got an error saying that it wasn't static. So I declared as static. static void setInput(QString accessInput); static QString accessInput; //both in the . h file Methods in the .cpp file are void TheIslandMain::setInput(QString input){ TheIslandMain::accessInput = input;} In the widget TheIslandMain::setInput(input); – Chris O'Brien Feb 25 '13 at 00:41
  • Hmm, not I have it down to an Unresolved External Symbol for the QString accessInput variable. Google doesn't seem to be particularly helpful – Chris O'Brien Feb 25 '13 at 00:53
  • If you declare it static, you still have to to create it. See [Unresolved external symbol on static class members](http://stackoverflow.com/questions/195207/unresolved-external-symbol-on-static-class-members) – Thomas Feb 25 '13 at 09:07
0

The .ui form is a widget. Just call show() on it.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • @ChrisO'Brien Yes. Create an instance of it first, of course. – Nikos C. Feb 24 '13 at 13:13
  • That's the problem. I have created a QWidget *panel, but the parameters for the constructor don't seem to allow me to link to the .ui file. – Chris O'Brien Feb 24 '13 at 13:15
  • @ChrisO'Brien You don't link to the ui file. You `#include` the header file of the form and then create an instance of the form's class. – Nikos C. Feb 24 '13 at 13:16
  • I'm still a bit lost. It's just the there doesn't seem to be an include for the initial main .ui file. All I can see is TheIslandMain::TheIslandMain(QWidget *parent) : QMainWindow(parent), ui(new Ui::TheIslandMain) That seems to be creating the initial ui. – Chris O'Brien Feb 24 '13 at 13:21
  • @ChrisO'Brien When you fist create a ui form, you get asked for the filenames of the *.h and *.cpp files for that form. That *.h file is what you have to include in all places where you want to create instances of that form. – Nikos C. Feb 24 '13 at 13:28
  • Thanks! I'll give that a go tomorrow! – Chris O'Brien Feb 24 '13 at 19:14