Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Making browser tabs
I was hoping to get some help on making tabs for my browser. I have read the documentation and to be honest I found it hard to grasp for n00bie like me. I placed the tabwidget in the ui and it has given me by default 2 tabs. How do I remove the second tab? I don't want any more than 1 tab until I press the 'new tab' button. How could I make a button create new tabs? Sorry for all the n00bie questions, I'm just trying to learn :)
I know its probably a dumb question, but help anyone?
If you are using Qt Creator,
First, to delete the second tab, you right click on your tab widget when the second tab is the active one then you go over "page 2 of 2" and choose "delete".
Then, to add one on your code you can do this :
where frame is the Frame you want inside this tab.
You can put a button that call addTab() on is slot clicked() but i'm affraid that if you want a chrome or firefox lookAlike button, you will need to overload QtabWidget to add this button
(or use the last tab and the slot currentChanged() of the tabWidget to add a new tab before the last one, and set this new tab as the current one but it's far to be clean)
ok it says that 'frame' is undefined. What do I do?
How do I choose a frame? And what exactly is a frame?
You should have a look at the documentation : "addTab(...)":http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/qtabwidget.html#addTab
my answer wan't really good, the first argument have to be a QWidget, in your case, a WebBrowser, it would be a QWebView, for exemple :
QWebView *myNewWebView = new QWebView();
ui->tabWidget->addTab(myNewWebView, "Tab 2");
but it could be any object that inherit from QWidget such as a Frame, a PushButton, ...
For your next question, a Frame is defined here "QFrame":http://harmattan-dev.nokia.com/docs/library/html/qt4/qframe.html
it's a kind of "container" where you can put other widgets, so you don't really have to chose one, you can create it in your code or from Qt Designer.
Ok so that kind of worked. I can press the 'new tab' button and it gives me a new tab that takes me to google, however the original tab doesnt automatically go to google and it doesnt resize to fill the screen. What should I do? And thanks for answering my noobie questions!
How can I make the new tab a clone of my old tab with search bar, buttons, etc.?
bq. however the original tab doesnt automatically go to google and it doesnt resize to fill the screen
I guess that you havn't defined the layout (it's what you use to order the widgets), it can be done in Qt Designer by right clicking on the tab and chose a Layout (the last option, i don't know how it is called because i'm french and my Qt is in french ;p, but you can chose, for example a gridLayout)
it will tell your tab how to order the widgets inside him.
You can take a look "here":http://qt-project.org/doc/qt-4.8/layout.html for more informations around Layouts.
You can also configure your layouts in your code.
Your first tab doesn't take you to google because you didn't asked him,
does it (obviously, you have to replace "myNewWebView" by your first webViw name.
If you want to have the same search bar, buttons, etc for all the tab, i suggest you put them outside the tab so they will always be there (but you will have to check which tab is active to do the different actions).
If you want them inside the tab, i think the easiest way to perform this is to add a new Graphical interface class in your project that inherit QFrame, and have all the features you want + a QWebView, and then add an instance of this class to every new tab.
Alright, I figured out the layouts. Basically it was glitched and restarting qt fixed the resizing problems. How can I make the buttons, search bar, etc. check which tab is active?
How can I add a new graphical interface class to my project and have it inherit QFrame, and add an instance every time I make a new tab?
bq. How can I make the buttons, search bar, etc. check which tab is active?
You can get this information with the method "currentIndex()":http://harmattan-dev.nokia.com/docs/library/html/qt4/qtabwidget.html#currentIndex-prop of tabWidget.
bq. How can I add a new graphical interface class to my project and have it inherit QFrame, and add an instance every time I make a new tab?
In my opinion, it would be a better solution.
First right click on you project directory, then choose "add new ..."
In the left side, choose "Qt"
In the right side choose the first one (it should be something like "Qt Designer graphical interface class" in english).
Then you will choose which class it will inherit from (QFrame is ok).
Then choose a name for this class like "myCustomTab", you will now have myCustomTab.cpp, myCustomTab.h and myCustomTab.ui in your project.
You can add the layout, buttons, searchbar, ... and the QWebView in myCustomTab.ui.
Now you just have to include "mycustomtab.h" in your class that have the QTabWidget (MainWindow i guess), delete all the tabs from the QTabWidget
bq. to delete a tab, you right click on your tab widget when this tab is the active one then you go over “page X of Y” and choose “delete”.
and when you want to add a new tab you call
MyCustomTab *tab = new MyCustomTab();
ui->tabWidget->addTab(tab, "new tab");
Hope it helps !
that all makes sense. Just one small problem: I'm not sure how to add the files without creating a whole new file. Where do I right click to 'add new'? I think I get the important stuff now!
Ok so I have added new files and I have used the code you provided, but it doesnt work. Some things appear to be undefined...help?